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:13

Using Swift 4 and Xcode 9

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

}
查看更多
美炸的是我
3楼-- · 2019-01-01 01:14

Small addition. For swift to compile w/o error, you need to add

import UIKit.UIGestureRecognizerSubclass

查看更多
谁念西风独自凉
4楼-- · 2019-01-01 01:24

Using Swift 3 and Xcode 8

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

}

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

}

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

}

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}
查看更多
何处买醉
5楼-- · 2019-01-01 01:25

It is now in the Apple API reference here and for overriding in xCode version 6.3 and swift 1.2 you can use this code:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch =  touches.first as? UITouch {
         // ...
    }
    // ...
}
查看更多
皆成旧梦
6楼-- · 2019-01-01 01:27

Swift 1.2 (Xcode 6.3) introduced a native Set type that bridges with NSSet. This is mentioned in the Swift blog and in the Xcode 6.3 release notes, but apparently not yet added to the official documentation (update: As Ahmad Ghadiri noted, it is documented now).

The UIResponder method is now declared as

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

and you can override it like this:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
        // ...
    }
    super.touchesBegan(touches , withEvent:event)
}

Update for Swift 2 (Xcode 7): (Compare Override func error in Swift 2)

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, withEvent:event)
}

Update for Swift 3:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}
查看更多
步步皆殇っ
7楼-- · 2019-01-01 01:28

What worked for me was:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            // ...
        }

        super.touchesBegan(touches , withEvent:event!)
    }
查看更多
登录 后发表回答