Justin Leonard
Justin Leonard

Reputation: 81

Overriding touchesBegan: Error - method does not override any method from superclass

I am trying to override touchesBegan in a custom subclass of UITapGestureRecognizer. Code is below. I got it from here: How to accelerate the identification of a single tap over a double tap?. It was the accepted answer, but i am getting an error: method does not override any method from its superclass. I have checked and this indeed seems to be the signature for touchesBegan. Help?

import UIKit

class UIShortTapGestureRecognizer: UITapGestureRecognizer {
    let tapMaxDelay: Double = 0.3

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        super.touchesBegan(touches, withEvent: event)
        delay(tapMaxDelay) {
        // Enough time has passed and the gesture was not recognized -> It has failed.
            if  self.state != UIGestureRecognizerState.Ended {
                self.state = UIGestureRecognizerState.Failed
            }
        }
    }


}

Upvotes: 2

Views: 2268

Answers (4)

Mujahid Latif
Mujahid Latif

Reputation: 616

For Swift 4.2 and XCode 10.2

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)

    }

Upvotes: 2

Justin Leonard
Justin Leonard

Reputation: 81

I was indeed using Xcode 7.2 with Swift 2.0. And removing the override keyboard was not the solution. Instead, I found that the solution was to add import UIKit.UIGestureRecognizerSubclass. That also allowed me to write over the state property, rather than it being read-only.

Upvotes: 0

onCompletion
onCompletion

Reputation: 6650

Please try to remove the override keyword. An override is possible when your superclass also implements that method too. In your case you are providing a version that replaces or modifies the behaviour of the superclass implementation. Which is not making the action over here I think.

On another hand if you are not using the upgraded version of swift like swift 2.0+ then please do upgrade your Xcode which will upgrade your Swift version too.

In my case this method looks like as follows. I am using Xcode 7.2 with Swift 2.1.1. Just as an example though -

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    testTouchMan(touches)
    let touch: UITouch! = touches.first as UITouch!
    let location = touch.locationInView(self.groundFloorView)
}

Upvotes: 0

Unheilig
Unheilig

Reputation: 16302

The problem stems from the fact that you are not using Xcode 7; your syntax is in Swift 1.2+ but is not supported by the Xcode version you are using.

If you are using Xcode version with Swift version prior to 1.2, you would need to change the method signature as follows:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent?)

Or, upgrade to Xcode 7.

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

became available only since Xcode 6.3.

Upvotes: 5

Related Questions