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

时间:2015-02-27 18:30:29

标签: ios swift override

Xcode 6.3。在实现UITextFieldDelegate协议的类中,我想重写touchesBegan()方法以隐藏键盘。如果我在函数规范中避免了编译器错误,那么在尝试读取" touch"时会出现编译错误。从Set或NSSet,或super.touchesBegan(touches,withEvent:event)抛出错误。在Xcode 6.2中编译的这些组合之一! (那么Swift的文档在哪里?#34; 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) 

编译器错误: 使用selector&#39; touchesBegan:withEvent:&#39;覆盖方法具有不兼容的类型&#39;(NSSet,UIEvent) - &gt; ()&#39; 和

super.touchesBegan(touches , withEvent:event)

也抱怨

&#39;的NSSet&#39;不能隐含地转换为&#39; Set&#39 ;;你的意思是使用&#39;作为&#39;明确转换?

尝试:

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

编译器错误:  输入&#39; AnyObject&#39;不符合协议&#39; Hashable&#39;

尝试:

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

编译错误

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

&#39;设置&#39;没有名为&#39; anyObject&#39;的成员但是功能规范和对super()的调用都没问题!

尝试:

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

编译器错误: 不能专门化非泛型类型&#39; NSSet&#39;

8 个答案:

答案 0 :(得分:160)

Swift 1.2(Xcode 6.3)引入了桥接的本地Set类型 与NSSet。这在Swift blogXcode 6.3 release notes中提到 Ahmad Ghadiri noted但显然尚未添加到官方文档(更新:作为Override func error in Swift 2现在已记录)。

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):(比较{{3}})

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

答案 1 :(得分:10)

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

答案 2 :(得分:10)

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

答案 3 :(得分:6)

它现在位于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 {
         // ...
    }
    // ...
}

答案 4 :(得分:4)

对我有用的是:

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

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

答案 5 :(得分:4)

小补充。对于swift编译没有错误,你需要添加

导入UIKit.UIGestureRecognizerSubclass

答案 6 :(得分:0)

使用 Swift 4 Xcode 9

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

}

答案 7 :(得分:0)

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