Overriding method with selector 'touchesBegan:

2019-01-01 00:54发布

Xcode 6.3. Within a class implementing UITextFieldDelegate protocol, I would like to override touchesBegan() method to possibly hide the keyboard. If I avoid a compiler error in the function spec, then there is a complier error trying to read the "touch" from the Set or NSSet, or else the super.touchesBegan(touches , withEvent:event) throws an error. One of these combinations compiled in Xcode 6.2! (So where is documentation to Swift "Set" and how to get an element from one?)

 override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    // Hiding the Keyboard when the User Taps the Background
        if let touch =  touches.anyObject() as? UITouch {
            if nameTF.isFirstResponder() && touch.view != nameTF {
                nameTF.resignFirstResponder();
            }
        }
        super.touchesBegan(touches , withEvent:event)
    }

Try:

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

Compiler error: Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()' and

super.touchesBegan(touches , withEvent:event)

also complains

'NSSet' is not implicitly convertible to 'Set'; did you mean to use 'as' to explicitly convert?

Try:

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

Compiler error: Type 'AnyObject' does not conform to protocol 'Hashable'

Try:

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

Compiler error at

if let touch = touches.anyObject() as? UITouch 

'Set' does not have a member named 'anyObject' BUT the function spec and call to super() are OK!

Try:

override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent) 

Compiler error: Cannot specialize non-generic type 'NSSet'

8条回答
与君花间醉酒
2楼-- · 2019-01-01 01:29

With xCode 7 and swift 2.0, use following code:

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

    if let touch =  touches.first{
        print("\(touch)")
    }
    super.touchesBegan(touches, withEvent: event)
}

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

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesEnded(touches, withEvent: event)
}

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

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesMoved(touches, withEvent: event)
}
查看更多
路过你的时光
3楼-- · 2019-01-01 01:38

The current one right now for the newest update as of xCode 7.2 Swift 2.1 on Dec 19, 2015.

Next time you get an error like this again, remove the function and start typing it again "touchesBe..." and xCode should automatically complete it to the newest one for you instead of trying to fix the old one.

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

    for touch: AnyObject! in touches {
        let touchLocation = touch.locationInNode(self)

        //Use touchLocation for example: button.containsPoint(touchLocation) meaning the user has pressed the button.
    }
}
查看更多
登录 后发表回答