前端之家收集整理的这篇文章主要介绍了
在Swift 2中覆盖func错误,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_
502_0@
这个
代码在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 */
}
当删除覆盖字时发生此错误:
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关键字,你得到的错误是让你知道,你已经做了一个方法,有一个冲突的Objective-C选择器与真正的touches开始方法(不像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的预发布版本中的轻量级泛型部分。 34。