2

In my SKScene subclass I have implemented a touchesBegan method. This method had the NSSet changed to Set in order to make it Swift 1.2 compatible (see this question).

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    // ...
}

Now the compiler gives me an error: Method does not override any method from its superclass. My code -as any Swift code- was broken in 1.2, and I have fixed every issue except this override case. Am I missing something here?

Community
  • 1
  • 1
klaevv
  • 457
  • 1
  • 7
  • 17
  • 1
    Duplicate of [Swift protocols: method does not override any method from its superclass](http://stackoverflow.com/questions/24380681/swift-protocols-method-does-not-override-any-method-from-its-superclass) ? Does removing the "override" keyword help? – Martin R May 22 '15 at 13:04
  • I need to use the override to know when a touch begins, right? So removing the override keyword makes the functionality fail. But yes, the project builds without overriding touchesBegan. – klaevv May 22 '15 at 13:08
  • How is your `SKScene` subclass declared? Something like `class MyScene: SKScene {` ? – vacawama May 22 '15 at 13:13
  • `touchesBegan:withEvent` is a method in `UIGestureRecognizer`. Are you defining your 'override' in a subclass of `UIGestureRecognizer`? – GoZoner May 22 '15 at 13:14
  • class MyScene: SKScene, OneAdDelegate { ..., that is the syntax I used – klaevv May 22 '15 at 13:15
  • touchesBegan used to work in SKScene subclasses, and from what I've read it still should – klaevv May 22 '15 at 13:16
  • 1
    @very_supercharged: Actually I don't get any error message with your exact code in a SKScene subclass, but it fails to compile if the override keyword is removed. This makes sense because SKScene inherits from UIResponder. – Are you sure that you don't have any typo in the method declaration? Is the above a copy/paste of your actual code? – Martin R May 22 '15 at 13:21
  • That's odd. I can compile without the override, but with it, an error occurs. The SKScene subclass is >700 lines long but the relevant parts -I assume- are posted here... – klaevv May 22 '15 at 13:25
  • Maybe you have an 'embedded control character'. Just recopy it from the UIResponder declaration. – GoZoner May 22 '15 at 13:29

2 Answers2

3

This worked for me

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

For more information about why is happening this error you can read from this answer that explain in detail about this: https://stackoverflow.com/a/30892467/2091181

Community
  • 1
  • 1
Jorge Casariego
  • 21,948
  • 6
  • 90
  • 97
1

So the "problem" had everything to do with the fact that the project had a Set class in it implemented, and I did not detect this redundancy. Quite a silly mistake.

klaevv
  • 457
  • 1
  • 7
  • 17
  • Can you clarify your answer and show how you fixed it? – Kat Jul 23 '15 at 20:42
  • I had a custom "Set" class implemented in my project. To fix the problem, I just changed "Set" class to "ASet" class, so the compiler dealt with the correct Swift Set class. – klaevv Jul 23 '15 at 23:10
  • 3
    Also as an FYI, for Swift 2.0 you want `override func touchesBegan(touches: Set, withEvent event: UIEvent?){}` – Kat Jul 24 '15 at 14:47