使用选择器 'touchesBegan:withEvent:' 的覆盖方法具有不兼容的类型 '(NSSet, UIEvent) -> ()'

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

Xcode 6.3。在实现 UITextFieldDelegate 协议的 class 中,我想覆盖 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) 

编译错误: 使用选择器 'touchesBegan:withEvent:' 的覆盖方法具有不兼容的类型 '(NSSet, UIEvent) -> ()' 和

super.touchesBegan(touches , withEvent:event)

也抱怨

'NSSet' 不能隐式转换为 'Set';您是要使用 'as' 来显式转换吗?

尝试:

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

编译错误: 类型 'AnyObject' 不符合协议 'Hashable'

尝试:

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

处的编译器错误
if let touch = touches.anyObject() as? UITouch 

'Set' 没有名为 'anyObject' 的成员,但是函数规范和对 super() 的调用都可以!

尝试:

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

编译错误: 无法专门化非泛型类型 'NSSet'

Swift 1.2 (Xcode 6.3) 引入了一种原生的 Set 类型,可以桥接 NSSet。这在 Swift blogXcode 6.3 release notes, but apparently not yet added to the official documentation (update: As ,现在 已记录)。

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) 的更新:(比较

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

Swift3 的更新:

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

对我有用的是:

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

        super.touchesBegan(touches , withEvent:event!)
    }

它现在在 Apple API 参考 here 中,要在 xCode 版本 6.3 和 swift 1.2 中覆盖,您可以使用此代码:

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

当前更新为 xCode 7.2 Swift 2.1 2015 年 12 月 19 日的最新更新。

下次你再次遇到这样的错误时,删除该函数并重新开始输入它 "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.
    }
}

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

小补充。对于swift编译w/o错误,需要添加

进口UIKit.UIGestureRecognizerSubclass

使用 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>
}

使用 Swift 4Xcode 9

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

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

        if let touch = touches.first as? UITouch {

            if touch.view == self.view{
                self.dismiss(animated: true, completion: nil)
            }
        }
    }