ios 使用选择器 'touchesBegan:withEvent:' 的覆盖方法具有不兼容的类型 '(NSSet, UIEvent) -> ()'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28771896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:02:11  来源:igfitidea点击:

Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()'

iosswiftoverriding

提问by SoCor

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?)

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)
    }

Try:

尝试:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) or
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent) 

Compiler error: Overriding method with selector 'touchesBegan:withEvent:' has incompatible type '(NSSet, UIEvent) -> ()' and

编译器错误:使用选择器 'touchesBegan:withEvent:' 的覆盖方法具有不兼容的类型 '(NSSet, UIEvent) -> ()' 和

super.touchesBegan(touches , withEvent:event)

also complains

还抱怨

'NSSet' is not implicitly convertible to 'Set'; did you mean to use 'as' to explicitly convert?

'NSSet' 不能隐式转换为 'Set';您的意思是使用“as”来显式转换吗?

Try:

尝试:

override func touchesBegan(touches: Set<AnyObject>, withEvent event: UIEvent) 

Compiler error: Type 'AnyObject' does not conform to protocol 'Hashable'

编译器错误:类型“AnyObject”不符合协议“Hashable”

Try:

尝试:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 

Compiler error at

编译器错误在

if let touch = touches.anyObject() as? UITouch 

'Set' does not have a member named 'anyObject' BUT the function spec and call to super() are OK!

'Set' 没有名为 'anyObject' 的成员,但函数规范和调用 super() 没问题!

Try:

尝试:

override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent) 

Compiler error: Cannot specialize non-generic type 'NSSet'

编译器错误:无法专门化非泛型类型“NSSet”

回答by Martin R

Swift 1.2 (Xcode 6.3)introduced a native Settype that bridges with NSSet. This is mentioned in the Swift blogand in the Xcode 6.3 release notes, but apparently not yet added to the official documentation(update: As Ahmad Ghadiri noted, it isdocumented now).

Swift 1.2 (Xcode 6.3)引入了一种SetNSSet. 这在Swift 博客Xcode 6.3 发行说明 中有所提及,但显然尚未添加到官方文档中(更新:正如Ahmad Ghadiri 所指出的,现在记录在案)。

The UIRespondermethod is now declared as

UIResponder方法现在声明为

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)

and you can override it like this:

你可以像这样覆盖它:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch = touches.first as? UITouch {
        // ...
    }
    super.touchesBegan(touches , withEvent:event)
}

Update for Swift 2 (Xcode 7):(Compare Override func error in Swift 2)

Swift 2 (Xcode 7) 的更新:(比较Swift 2 中的 Override func 错误

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, withEvent:event)
}

Update for Swift 3:

Swift 3 更新:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        // ...
    }
    super.touchesBegan(touches, with: event)
}

回答by Himanshu Mahajan

With xCode 7 and swift 2.0, use following code:

使用 xCode 7 和 swift 2.0,使用以下代码:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch =  touches.first{
        print("\(touch)")
    }
    super.touchesBegan(touches, withEvent: event)
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesEnded(touches, withEvent: event)
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    if let touch = touches.first{
        print("\(touch)")
    }
    super.touchesMoved(touches, withEvent: event)
}

回答by Chetan Prajapati

Using Swift 3and Xcode 8

使用Swift 3Xcode 8

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {

}

override func touchesCancelled(_ touches: Set<UITouch>?, with event: UIEvent?) {
// Don't forget to add "?" after Set<UITouch>
}

回答by Ahmad Ghadiri

It is now in the Apple API reference hereand for overriding in xCode version 6.3 and swift 1.2 you can use this code:

它现在在此处的 Apple API 参考中,要在 xCode 6.3 版和 swift 1.2 版中进行覆盖,您可以使用以下代码:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    if let touch =  touches.first as? UITouch {
         // ...
    }
    // ...
}

回答by Jose Ramirez

The current one right now for the newest update as of xCode 7.2 Swift 2.1 on Dec 19, 2015.

当前的最新更新,截至 2015 年 12 月 19 日的 xCode 7.2 Swift 2.1。

Next time you get an error like this again, remove the function and start typing it again "touchesBe..." and xCode should automatically complete it to the newest one for you instead of trying to fix the old one.

下次您再次遇到这样的错误时,删除该函数并再次开始输入“touchesBe...”,xCode 应该会自动为您将其补全到最新的,而不是尝试修复旧的。

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject! in touches {
        let touchLocation = touch.locationInNode(self)

        //Use touchLocation for example: button.containsPoint(touchLocation) meaning the user has pressed the button.
    }
}

回答by jfredsilva

What worked for me was:

对我有用的是:

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touch = touches.first as? UITouch {
            // ...
        }

        super.touchesBegan(touches , withEvent:event!)
    }

回答by beshio

Small addition. For swift to compile w/o error, you need to add

小补充。为了快速编译没有错误,您需要添加

import UIKit.UIGestureRecognizerSubclass

导入 UIKit.UIGestureRecognizerSubclass

回答by ammar shahid

Using Swift 4and Xcode 9

使用Swift 4Xcode 9

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

}

回答by Talha Rasool

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        if let touch = touches.first as? UITouch {

            if touch.view == self.view{
                self.dismiss(animated: true, completion: nil)
            }
        }
    }