Swift 2 中的覆盖功能错误
Override func error in Swift 2
这段代码在XCode 6 中没有错误但是在XCode 7 (Swift 2) 中出现了这个错误:
Method does not override any method from its superclass
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
删除 override
字时出现此错误:
Method 'touchesBegan(:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector
您遇到第一个错误是因为 Cocoa Touch 的大部分内容已经过审核以支持 Objective-C 泛型,这意味着现在可以输入数组和集合等元素。因此,此方法的签名已更改,并且由于您编写的内容不再与此匹配,因此您会收到一条错误消息,说明您已将方法标记为 override
但事实并非如此实际上匹配 super class.
中的任何方法
然后当您删除 override
关键字时,您得到的错误是让您知道您创建的方法具有与真正的 touches begin 方法冲突的 Objective-C 选择器(不像Swift,Objective-C不支持方法重载)。
最重要的是,在 Swift 2 中,您的触摸开始覆盖应该如下所示。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// stuff
}
有关 Objective-C 泛型对您的 Swift 代码意味着什么的更多信息,我建议您查看 Using Swift with Cocoa and Objective-C 预发行版中的轻量级泛型部分。截至目前第 33 和 34 页。
只需删除覆盖即可。
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
这段代码在XCode 6 中没有错误但是在XCode 7 (Swift 2) 中出现了这个错误:
Method does not override any method from its superclass
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}
删除 override
字时出现此错误:
Method 'touchesBegan(:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector
您遇到第一个错误是因为 Cocoa Touch 的大部分内容已经过审核以支持 Objective-C 泛型,这意味着现在可以输入数组和集合等元素。因此,此方法的签名已更改,并且由于您编写的内容不再与此匹配,因此您会收到一条错误消息,说明您已将方法标记为 override
但事实并非如此实际上匹配 super class.
然后当您删除 override
关键字时,您得到的错误是让您知道您创建的方法具有与真正的 touches begin 方法冲突的 Objective-C 选择器(不像Swift,Objective-C不支持方法重载)。
最重要的是,在 Swift 2 中,您的触摸开始覆盖应该如下所示。
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// stuff
}
有关 Objective-C 泛型对您的 Swift 代码意味着什么的更多信息,我建议您查看 Using Swift with Cocoa and Objective-C 预发行版中的轻量级泛型部分。截至目前第 33 和 34 页。
只需删除覆盖即可。
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
/* Called when a touch begins */
}