Compiler error: 写Swift报出 Obj-C 选择器不允许重载
来源:互联网 发布:网络教育和自考 编辑:程序博客网 时间:2024/06/05 23:39
Compiler error:Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector
在跟着上斯坦福的Developing iOS 8 Apps with Swift 课程,在用Swift重写方法时发生错误,报错代码如下:
class ViewController: UIViewContoller{ func performOperation(operation: (Double,Double) -> Double){} func performOperation(operation: Double -> Double){}}
然后报出的编译错误是:
Compiler error:
Method with Objective-C selector conflicts with previous declaration with the same Objective-C selector
给出一个解决方法:
Solution:
将其中一个方法 转为私有方法:
private func performOperation(operation: Double -> Double){}
报错原因
Reason
Obj-C不支持方法的重载(Overloading),所以得用不同的方法名来写方法;当方法继承UIViewController,就继承了NSObject,这是Obj-C的类,所以你写相同方法名的时候会报错;
Swift支持方法的重载,所以你不继承Obj-C类时,编译器不报错;
去除UIViewController时的情况不报错 如下:
class ViewController{ func performOperation(operation: (Double,Double) -> Double){} func performOperation(operation: Double -> Double){}}
关于Xcode版本与Swift的变化可以看这个链接:
https://developer.apple.com/library/ios/releasenotes/DeveloperTools/RN-Xcode/Chapters/xc6_release_notes.html#//apple_ref/doc/uid/TP40001051-CH4-SW1
0 0
- Compiler error: 写Swift报出 Obj-C 选择器不允许重载
- error C2733: 不允许重载函数“write_string”的第二个 C 链接
- error C2733: 不允许重载函数“DllGetClassObject”的第二个 C 链接
- error C2733: 不允许重载函数“GetPoint”的第二个 C 链接
- C++:internal compiler error解决
- 同时使用Swift和Obj-C
- Swift Compiler Error Integer literal overflows when stored into 'UInt8'
- error: C compiler cannot create executables
- configure: error: C compiler cannot create executables
- configure: error: C compiler cannot create executables
- iOS 的 c++,c,obj-C,swift 混用 (转载)
- 不允许重载函数“_interlockedbittestandreset”的第二个 C 链
- swift调用obj-c要注意的小问题
- 在swift项目中引入obj-c代码的注意事项
- ios 删除线的obj-c实现和swift实现
- elipse 报出An internal error occurred during: "C/C++ Indexer". java.lang.NullPointerExcption 错误
- Object-C--->Swift之(七)函数重载
- checking for C compiler default output file name... configure: error: C compiler cannot create executables
- SpringMVC接受复杂json数据类型
- Centos上源码安装Hadoop2.7.2
- future给任务设置时限
- js调用php和php调用js的方法举例
- OpenCV学习:人脸识别 简单程序
- Compiler error: 写Swift报出 Obj-C 选择器不允许重载
- 一个关于android游戏下载静默安装功能的思路以及实现
- 欧拉环、欧拉路径的判定和求法
- md5加密
- redis 主备哨兵模式
- 指针变量
- Android开发,美到超乎你想象的android开源交互动画和视觉效果,外部库
- 面试中的赋值运算符函数
- HTML页面头部信息