'Set <NSObject>'没有一个名为'anyObject'的成员 – Xcode 6.3

我正在检查是否已经select了一个元素。

func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { // First, see if the game is in a paused state if !gamePaused { // Declare the touched symbol and its location on the screen let touch = touches.anyObject! as? UITouch let location = touch.locationInNode(symbolsLayer) 

而这之前已经在Xcode 6.2中编译好了,但是在6.3更新的时候,“let touch = touches.anyObject!as?UITouch”就是抛出这个错误:

'Set'没有名为'anyObject'的成员

我已经阅读了许多类似的问题,但我似乎无法绕过我的头。“要使用这个值,你需要先”解开“它。 特别是因为答案似乎集中在通知。

非常感谢。 w ^

 let touch = touches.first as? UITouch 

.first可以让你访问UITouch的第一个对象。

由于Xcode 6.3使用了Swift(1.2)的更新版本,所以你需要将你的旧代码转换成Swift 1.2(编辑 – >转换 – >到最新的Swift)。

Swift 1.2使用Set (Swift中的新特性),而不是使用NSSet (Objective-C中的旧版本)。 因此,触摸开关function也将其参数从NSSet改变为Set。

欲了解更多信息, 请参阅此

这将检查symbolsLayer中的多个触摸

 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { // First, see if the game is in a paused state if !gamePaused { // Declare the touched symbol and its location on the screen for touch: AnyObject in touches { let location = (touch as! UITouch).locationInNode(symbolsLayer) } } }