使用select器'touchesBegan:withEvent:'具有不兼容types'(NSSet,UIEvent) – >()'的覆盖方法

Xcode 6.3。 在实现UITextFieldDelegate协议的类中,我想重写touchesBegan()方法来隐藏键盘。 如果我避免了函数规范中的编译器错误,那么在尝试从Set或NSSet中读取“touch”时会出现编译器错误,否则super.touchesBegan(touches,withEvent:event)将引发错误。 在Xcode 6.2中编译的这些组合之一! (所以Swift“Set”的文档以及如何从中获取元素?)

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) } 

尝试:

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

编译器错误:使用select器'touchesBegan:withEvent:'具有不兼容types'(NSSet,UIEvent) – >()'的覆盖方法

 super.touchesBegan(touches , withEvent:event) 

也抱怨

'NSSet'不能隐式转换为'Set'; 你的意思是用“as”来明确转换吗?

尝试:

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

编译器错误:types'AnyObject'不符合协议'Hashable'

尝试:

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

编译器错误在

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

'Set'没有名为'anyObject'的成员,但函数spec和super()调用都OK!

尝试:

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

编译器错误:无法专门化非genericstypes'NSSet'

Swift 1.2(Xcode 6.3)引入了与NSSet桥接的本地Settypes。 这在Swift博客和Xcode 6.3发行说明中提到 , 但显然还没有添加到官方文档中 (更新:正如Ahmad Ghadiri指出的那样 ,现在已经logging)。

UIResponder方法现在被声明为

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

你可以像这样覆盖它:

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

更新Swift 2(Xcode 7):(比较Swift 2中的Override func错误 )

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

Swift 3的更新:

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

使用xCode 7和swift 2.0,使用以下代码:

 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) } 

现在是在这里的苹果API的参考和重写在xCode版本6.3和SWIFT 1.2你可以使用这个代码:

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

使用Swift 3Xcode 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> } 

目前正在进行最新的更新,截至2015年12月19日的xCode 7.2 Swift 2.1。

下次再次出现这样的错误时,请删除该函数并重新input“touchesBe …”,并且xCode会自动将它完成为最新版本,而不是尝试修复旧版本。

 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. } } 

对我有效的是:

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

小额外。 为了迅速编译W / O错误,你需要添加

导入UIKit.UIGestureRecognizerSubclass