您的位置:首页 > 移动开发 > Swift

swift2.x不能使用函数重载的问题

2016-03-16 18:33 483 查看
最近在看斯坦福大学ios开发教程时,一边看一边跟着敲代码,结果发现函数不能重载:



错误内容是:

Method ‘performOperation’ with Objective-C selector

‘performOperation:’ conflicts with previous declaration with the same

Objective-C selector

最后在这个博客找到了原因:

这是因为你的viewcontroller 继承了UIViewController.而UIViewController

继承自oc的NSObject. 在swift 中被修饰成@objc class.

那么就必须要遵循oc的selector,在oc中是不支持方法重载的。所以会报上面的错误。这跟使用的Xcode版本有关,白胡子老头使用的版本较低,而我使用的是Xcode7,已经是Swift2了,与之前有好多不同的地方。

在Compiler error: Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector找到了解决办法:

在Xcode7的Swift2中有两种解决方法:

一种是使用@objc(newNameMethod:),如下

func methodOne(par1, par2) {…}

@objc(methodTow:) func methodOne(par1) {…}

另一种是使用 @nonobjc,如下:

func methodOne() {…}

@nonobjc func methodOne() {…}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: