Overriding method with selector #39;touchesBegan:withEvent:#39; has incompatible type #39;(NSSet, UIEvent) -gt; ()#39;(选择器“touchesBegan:withEvent:的覆盖方法具有不兼容的类型“(NSSet,UIEvent)-())
问题描述
Xcode 6.3.在实现 UITextFieldDelegate 协议的类中,我想重写 touchesBegan() 方法以可能隐藏键盘.如果我在函数规范中避免了编译器错误,则尝试从 Set 或 NSSet 读取触摸"时会出现编译器错误,否则 super.touchesBegan(touches , withEvent:event) 会引发错误.在 Xcode 6.2 中编译的这些组合之一!(那么 Swift "Set" 的文档在哪里以及如何从中获取元素?)
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)->()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选择器“touchesBegan:withEvent:"的覆盖方法具有不兼容的类型“(NSSet,UIEvent)->()'
				
        
 
            
        - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 - Android - 拆分 Drawable 2022-01-01
 - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 
				
				
				
				