swift函数重载 [英] function overloading in swift

查看:55
本文介绍了swift函数重载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下参数创建两个方法,但编译器抱怨它们不明确.我正在关注一个 youtube 系列来学习 Swift,在那里的视频中似乎没问题.我错过了什么?

I am trying to create two methods with the following arguments but the compiler is complaining that they are ambiguous. I am following a youtube series to learn Swift and it seems to be fine in the video there. What am I missing?

func performOperation(operation: Double -> Double){

func performOperation(operation:(Double, Double)->Double){

推荐答案

看来您可能遇到了 这个 reddit 线程.由于与Objective C方法的继承冲突,解决方案似乎只是重命名您的函数.

It seems you may be running into the same problem mentioned in this reddit thread. It looks like the solution may just be to rename your functions, due to a clash with inherited with Objective C methods.

例如

func myPerformOperation(operation: Double -> Double){

func myPerformOperation(operation:(Double, Double)->Double){

(这些不是理想的名称——它们应该更具描述性——但你懂的.)

(These aren't ideal names—they should be more descriptive—but you get the idea.)

这篇关于swift函数重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆