加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在Swift 2中覆盖func错误

发布时间:2020-12-14 06:08:41 所属栏目:百科 来源:网络整理
导读:这个代码在XCode 6没有错误,但在XCode 7(Swift 2)发生此错误: Method does not override any method from its superclass override func touchesBegan(touches: SetNSObject,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 */

}

当删除覆盖字时发生此错误:

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。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读