The overriding method with the selector 'touchhesBegan: withEvent:' has an incompatible type '(NSSet, UIEvent) -> ()'

Xcode 6.3. Inside a class that implements the UITextFieldDelegate protocol, I would like to override the touchhesBegan () method to hide the keyboard. If I avoid a compiler error in the spec function, an error occurs with the integrator trying to read the β€œtouch” from the set or NSSet, otherwise super.touchesBegan (concerns, withEvent: event) throws an error. One of these combinations compiled in Xcode 6.2! (So, where is the documentation for 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 'touchhesBegan: withEvent:' has an incompatible type '(NSSet, UIEvent) β†’ ()' and

 super.touchesBegan(touches , withEvent:event) 

also complains

'NSSet' is implicitly converted to 'Set'; did you want to use 'as' for explicit conversion?

Try:

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

Compiler Error: Type "AnyObject" does not conform to the "Hashable" protocol

Try:

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

Compiler error in

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

'Set' does not have a member named 'anyObject', but the spec function and calling super () are fine!

Try:

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

Compiler Error: Unable to specialize non-standard type "NSSet"

+64
override ios swift
Feb 27 '15 at 18:30
source share
7 answers

Swift 1.2 (Xcode 6.3) introduced its own type Set , which connects bridges to NSSet . This is mentioned in the Swift blog and in Xcode 6.3 release notes , but apparently has not yet been added to the official documentation (update: As Ahmad Gadiri noted , this is documented now).

Now the UIResponder method UIResponder 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 Func error override 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) } 
+151
Feb 27 '15 at 18:46
source share

Using xCode 7 and swift 2.0, use the 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) } 
+8
Oct. 14 '15 at 2:57
source share

Now it is in the Apple API reference 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
Apr 09 '15 at 21:37
source share

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> } 
+6
Sep 20 '16 at 17:26
source share

The current version is now for the latest update from xCode 7.2 Swift 2.1 dated December 19, 2015.

The next time you get this error again, delete this function and start typing it again "touchhesBe ...", and xCode should automatically finish it to the newest 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. } } 
+5
Sep 20 '15 at 1:38
source share

What worked for me:

  override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { if let touch = touches.first as? UITouch { // ... } super.touchesBegan(touches , withEvent:event!) } 
+3
Apr 08 '15 at 22:10
source share

A small addition. For fast compilation without error you need to add

import UIKit.UIGestureRecognizerSubclass

+3
May 08 '16 at
source share



All Articles