'Set <NSObject>'没有名为'anyObject'的成员. - Xcode 6.3

Wil*_*lie 23 xcode set nsobject ios swift

我正在检查是否已选择元素.

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

这个以前在Xcode 6.2中编译得很好但是有了6.3更新,"let touch = touches.anyObject!as?UITouch"这一行引发了错误:

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

我已经阅读了许多类似的问题,但我似乎无法理解"要使用价值,你需要首先解开".特别是因为答案似乎集中在通知上.

非常感谢.w ^

小智 22

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

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

由于Xcode 6.3使用Swift(1.2)的更新版本,您需要将旧代码转换为Swift 1.2(编辑 - >转换 - >最新Swift).

Swift 1.2,使用Set's(Swift中的新)而不是使用NSSet's(Objective-C中的旧版本).因此,touchbegan函数也将其参数从NSSet更改为Set.

有关详细信息,请参阅此处

  • 谢谢.你能编辑你的答案并添加相同的答案吗?并解释更多,所以每个人都理解.SO不仅仅是为了分享链接,而是解释并提供参考链接 (2认同)