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

SoC*_*Cor 76 overriding ios swift

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 touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) 
Run Code Online (Sandbox Code Playgroud)

编译错误

if let touch = touches.anyObject() as? UITouch 
Run Code Online (Sandbox Code Playgroud)

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

尝试:

override func touchesBegan(touches: NSSet<AnyObject>, withEvent event: UIEvent) -> () or
override func touchesBegan(touches: NSSet<NSObject>, withEvent event: UIEvent) 
Run Code Online (Sandbox Code Playgroud)

编译错误:无法专门化非泛型类型'NSSet'

Mar*_*n R 161

Swift 1.2(Xcode 6.3)引入了Set与之桥接的本机类型NSSet.这在Swift博客Xcode 6.3发行说明中有所提及,但显然尚未添加到官方文档中(更新:正如Ahmad Ghadiri所说,现在已经记录在案).

UIResponder方法现在声明为

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent)
Run Code Online (Sandbox Code Playgroud)

你可以像这样覆盖它:

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

更新Swift 2(Xcode 7):(比较Swift 2中的覆盖功能错误)

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

Swift 3更新:

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


Him*_*jan 10

使用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)
}
Run Code Online (Sandbox Code Playgroud)


Che*_*ati 10

使用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>
}
Run Code Online (Sandbox Code Playgroud)


Ahm*_*iri 6

它现在在Apple API参考中,并且在xCode版本6.3和swift 1.2中重写,您可以使用此代码:

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


Jos*_*rez 6

现在是2015年12月19日xCode 7.2 Swift 2.1的最新版本.

下次再次出现这样的错误时,删除该功能并再次开始输入"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.
    }
}
Run Code Online (Sandbox Code Playgroud)