【发布时间】:2015-04-30 13:37:57
【问题描述】:
Xcode 6.3。在实现 UITextFieldDelegate 协议的类中,我想覆盖 touchesBegan() 方法以可能隐藏键盘。如果我在函数规范中避免了编译器错误,则尝试从 Set 或 NSSet 读取“触摸”时会出现编译器错误,否则 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”
【问题讨论】:
标签: ios swift overriding