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)
}
Run Code Online (Sandbox Code Playgroud)
尝试:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent)
Run Code Online (Sandbox Code Playgroud)
编译器错误:使用选择器'touchesBegan:withEvent:'的覆盖方法具有不兼容的类型'(NSSet,UIEvent) - >()'和
super.touchesBegan(touches , withEvent:event)
Run Code Online (Sandbox Code Playgroud)
还抱怨
'NSSet'不能隐式转换为'Set'; 你的意思是使用'as'来明确转换?
尝试:
override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent)
Run Code Online (Sandbox Code Playgroud)
编译器错误:类型'AnyObject'不符合协议'Hashable'
尝试:
override func …Run Code Online (Sandbox Code Playgroud)