在Swift 2中重写func错误 [英] Override func error in Swift 2

查看:77
本文介绍了在Swift 2中重写func错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

XCode 6中的此代码没有错误,但在XCode 7(Swift 2)中已发生此错误:

This code in XCode 6 does not have error but in XCode 7 (Swift 2) this error has occurred :

方法不会覆盖其超类中的任何方法

Method does not override any method from its superclass

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        /* Called when a touch begins */

}

删除override字时,发生此错误:

When remove override word this error occurred :

带有Objective-C选择器'touchesBegan:withEvent:'的方法'touchesBegan(:withEvent :)'与具有相同Objective的超类'UIResponder'中的方法'touchesBegan(:withEvent :)'发生冲突-C选择器

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,但实际上与该方法不匹配超类的任何方法.

You're getting your first error because much of Cocoa Touch has been audited to support Objective-C generics, meaning elements of things like arrays and sets can now be typed. As a result of this, the signature of this method has changed and since what you've written no longer matches this, you're given an error explaining that you've marked a method as override but it doesn't actually match any methods from the super class.

然后,当您删除override关键字时,出现的错误是让您知道您所创建的方法具有与真正的touched begin方法冲突的Objective-C选择器(与Swift,Objective- C不支持方法重载).

Then when you remove the override keyword, the error you're getting is letting you know that you've made a method that has a conflicting Objective-C selector with the real touches began method (unlike Swift, Objective-C doesn't support method overloading).

在Swift 2中,最重要的是,您的触摸开始覆盖应该看起来像这样.

The bottom line is, in Swift 2, your touches began override should look like this.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    // stuff
}

有关Objective-C泛型对您的Swift代码的含义的更多信息,建议您查看

For more information on what Objective-C generics mean for your Swift code, I suggest you take a look at the Lightweight Generics section in the prerelease version of Using Swift with Cocoa and Objective-C. As of now on pages 33 & 34.

这篇关于在Swift 2中重写func错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆