Compiler error: 写Swift报出 Obj-C 选择器不允许重载
Posted 文恒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Compiler error: 写Swift报出 Obj-C 选择器不允许重载相关的知识,希望对你有一定的参考价值。
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
以上是关于Compiler error: 写Swift报出 Obj-C 选择器不允许重载的主要内容,如果未能解决你的问题,请参考以下文章
fatal error C1189: #error: STL1003: Unexpected compiler, expected C++ compiler
Error:scalac: Error: Error compiling the sbt component ‘compiler-interface-2.11.8-55.0‘
ERROR 错误:Angular JIT 编译失败:'@angular/compiler' 未加载
IDEA Error:java: Compilation failed: internal java compiler error
idea报错:error java compilation failed internal java compiler error
(Idea) Error:java: Compilation failed: internal java compiler error