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) 我正在检查是否已选择元素.
func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
{
// First, see if the game is in a paused state
if !gamePaused
{
// Declare the touched symbol and its location on the screen
let touch = touches.anyObject! as? UITouch
let location = touch.locationInNode(symbolsLayer)
Run Code Online (Sandbox Code Playgroud)
这个以前在Xcode 6.2中编译得很好但是有了6.3更新,"let touch = touches.anyObject!as?UITouch"这一行引发了错误:
'Set'没有名为'anyObject'的成员
我已经阅读了许多类似的问题,但我似乎无法理解"要使用价值,你需要首先解开".特别是因为答案似乎集中在通知上.
非常感谢.w ^