问题描述
我开始学习 Swift,并且一直在关注 YouTube 上非常棒的斯坦福大学视频讲座.如果您有兴趣或有帮助,这里有一个链接(尽管理解我的问题不是必需的):
I am starting to learn Swift, and have been following the very good Stanford University video lectures on YouTube. Here is a link if you are interested or it helps (although it isn't required to understand my problem):
开发 iOS 8 应用使用 Swift - 2. 更多 Xcode 和 Swift、MVC
在听完讲座后,我发现(据我所知)我的代码与视频中的代码相同,但在我的系统上我遇到了编译器错误.经过大量的反复试验,我设法将我的代码减少到两个示例,其中一个生成错误,另一个生成错误,但我不知道实际导致错误的原因或如何解决它.
While following the lectures I got to a point where (as far as I could tell) my code was identical to the code in the video but on my system I got a compiler error. After a lot of trial and error I have managed to reduce my code to two examples, one of which generates an error, the other or which doesn't, but I have no idea what is actually causing the error or how to resolve it.
产生错误的代码是:
import UIKit
class BugViewController: UIViewController
{
func perform(operation: (Double) -> Double) {
}
func perform(operation: (Double, Double) -> Double) {
}
}
这会产生以下编译器错误:
This creates the following compiler error:
使用 Objective-C 选择器 'perform:' 的方法 'perform' 与具有相同 Objective-C 选择器的先前声明冲突
通过简单地删除 UIViewController 的子类,代码编译:
By simply removing the sub-classing of UIViewController the code compiles:
import UIKit
class BugViewController
{
func perform(operation: (Double) -> Double) {
}
func perform(operation: (Double, Double) -> Double) {
}
}
其他一些可能相关或不相关的信息:
Some other information which may or may not be relevant:
- 我最近升级到了优胜美地.
- 当我安装 Xcode 时,我最终得到了 Beta 版本(版本 6.3 (6D543q)),因为(如果我没记错的话)这是我需要在我的 OS X 版本上运行的版本.
我有点希望这是编译器中的一个错误,否则这对我来说没有任何意义.非常感谢您的帮助!
I am half hoping this is a bug in the compiler because otherwise this doesn't make any sense to me. Any help very gratefully received!
推荐答案
Objective-C 不支持方法重载,您必须使用不同的方法名称.当您继承 UIViewController 时,您继承了 NSObject 并使该类可与 Obj-C 互操作.另一方面,Swift 确实支持重载,这就是为什么当您删除继承时它可以工作的原因.
Objective-C does not support method overloading, you have to use a different method name. When you inherited UIViewController you inherited NSObject and made the class interopable to Obj-C. Swift on the other hand does support overloading, that's why it works when you remove the inheritance.
这篇关于编译器错误:具有 Objective-C 选择器的方法与具有相同 Objective-C 选择器的先前声明冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!