2

当我实现这个方法时,我终止了 SourceKitService:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!)

如果我删除 , 有用。但是后来我的代码当然出错了。完整代码:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {

    for touch: AnyObject in touches {

        let location = touch.locationInNode(self)

        if nodeAtPoint(location).name == "plane" {
            println("plane touched")
        }
    }
}
4

2 回答 2

0

这个错误也发生在我身上。我现在解决了它,首先将 NSSet 转换为 NSArray,然后使用 for..in 循环方式。这没有崩溃:

override func touchesBegan(touches: NSSet!, withEvent event: UIEvent!) {

        super.touchesBegan(touches, withEvent: event)

        let touchArray: NSArray = touches.allObjects

        for element in touchArray {
            println("\(element)")
        }

    }
于 2014-08-07T11:03:47.367 回答
0

在 beta4 上运行,我能够毫无问题地声明该方法。for touch: AnyObject in touches {看起来是问题所在,因为当我替换AnyObjectUITouch更具体到您想要获得的内容时,我得到了编译器错误:NSSet! cannot be implicitly downcast to UITouch,这意味着语法试图将数组转换为类型。似乎还不可能铸造该物品。请参阅带有数组和 UIView 的 Swift for 循环中的注释错误。这是没有编译器错误的相同正文代码:

for item in touches {

    let location = (item as UITouch).locationInNode(self)    

    if self.nodeAtPoint(location).name == "plane" {
        println("plane touched")
    }

}

这些错误不足以触发我的 SourceKit 崩溃。也许您在代码中还有其他问题区域?

于 2014-08-07T06:45:04.237 回答