Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
488 views
in Technique[技术] by (71.8m points)

swift2 - Override func error in 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 */

}

When remove override word this error occurred :

Method 'touchesBegan(:withEvent:)' with Objective-C selector 'touchesBegan:withEvent:' conflicts with method 'touchesBegan(:withEvent:)' from superclass 'UIResponder' with the same Objective-C selector

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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).

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
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...