使用选择器'touchesBegan:withEvent:'的覆盖方法具有不兼容的类型'(NSSet,UIEvent) - > ()” [英] Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'
问题描述
Xcode 6.3。在实现UITextFieldDelegate协议的类中,我想重写touchesBegan()方法以隐藏键盘。如果我在函数规范中避免了编译器错误,那么尝试从Set或NSSet中读取touch时会出现编译器错误,否则super.touchesBegan(touches,withEvent:event)会抛出错误。在Xcode 6.2中编译的这些组合之一! (那么SwiftSet的文档在哪里以及如何从一个元素中获取元素?)
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)
}
尝试:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
编译器错误:
使用选择器'touchesBegan:withEvent:'的覆盖方法具有不兼容的类型'(NSSet,UIEvent) - >()'
和
Compiler error: Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()' and
super.touchesBegan(touches , withEvent:event)
还抱怨
'NSSet'不能隐式转换为'Set';你的意思是使用'as'来明确转换吗?
'NSSet' is not implicitly convertible to 'Set'; did you mean to use 'as' to explicitly convert?
尝试:
override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent)
编译器错误:
类型'AnyObject'不符合协议'Hashable'
Compiler error: Type 'AnyObject' does not conform to protocol 'Hashable'
尝试:
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
编译错误
if let touch = touches.anyObject() as? UITouch
'Set'没有名为'anyObject'的成员但是函数规范和调用super ()没问题!
'Set' does not have a member named 'anyObject' BUT the function spec and call to super() are OK!
尝试:
override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent)
编译器错误:
无法专门化非泛型类型'NSSet'
Compiler error: Cannot specialize non-generic type 'NSSet'
推荐答案
Swift 1.2(Xcode 6.3)引入了一个本地 Set
类型,它将
与 NSSet
连接起来。这在 Swift博客和
Xcode 6.3发行说明,但是显然尚未添加到官方文档中(更新:正如 Ahmad Ghadiri所指出的那样,它是现在记录)。
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).
现在将 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中的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)
}
这篇关于使用选择器'touchesBegan:withEvent:'的覆盖方法具有不兼容的类型'(NSSet,UIEvent) - > ()”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!