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