简体   繁体   English

touches.anyObject()不再起作用

[英]touches.anyObject() no longer working

I've got an error that I don't know how to handle. 我有一个我不知道该如何处理的错误。 In

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

        //Some code here

        let touch = touches.anyObject() as! UITouch
        let touchLocation = touch.locationInNode(self)
        sceneTouched(touchLocation)

}

I get the error "Set" does not have a member named 'anyObject'". I've searched all along the internet and found nothing. I know it is related with some recent changes in Swift but I don't know how to aproach it. Any help will be appreciated! 我收到错误“集”没有名为“ anyObject”的成员。我一直在互联网上搜索但一无所获。我知道它与Swift中的某些最新更改有关,但我不知道该如何处理它,任何帮助将不胜感激!

Try this 尝试这个

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    var touch : UITouch!
    touch = touches.first as? UITouch
    let touchLocation = touch.locationInNode(self)
    sceneTouched(touchLocation)
 }

Note that Swift Set type does not have anyObject method. 请注意,Swift Set类型没有anyObject方法。 You can either cast it to NSSet and then use anyObject method or simply use first? 您可以将其强制转换为NSSet,然后使用anyObject方法,也可以仅使用first? that is available to all CollectionType in Swift. 可供Swift中的所有CollectionType使用。

let touch = touches.first as! UITouch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM