快速错误 - 使用未申报类型

时间:2015-07-14 17:03:41

标签: ios swift

我正在Swift中构建“太空入侵者”的复制品。我得到的错误:

  

使用未声明的类型'设置'

以下是我的代码示例:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)
    if(touchedNode.name == "startgame"){
        let gameOverScene = GameScene(size: size)
        gameOverScene.scaleMode = scaleMode
        let transitionType = SKTransition.flipHorizontalWithDuration(1.0)
        view?.presentScene(gameOverScene,transition: transitionType)
    }
}

和:

 override func touchesBegan(touches: Set <NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    for touch in (touches as Set<UITouch>) {
        let location = touch.locationInNode(self)
        let sprite = SKSpriteNode(imageNamed:"Spaceship")

        sprite.xScale = 0.5
        sprite.yScale = 0.5
        sprite.position = location

        let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

        sprite.runAction(SKAction.repeatActionForever(action))

        self.addChild(sprite)
    }
}

在这两种情况下,错误都发生在:(touches: Set<NSObject>, withEvent event: UIEvent)

有人可以建议一个可能的解决方案吗?

2 个答案:

答案 0 :(得分:1)

我认为您使用新版本的swift更新了代码。

此代码适用于旧版本:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
}
  

Swift 1.2中引入的设置

来自Xcode 6.3.2版本:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    super.touchesBegan(touches, withEvent: event)
    var touch : UITouch = (touches.first as? UITouch)!

}

来自Xcode 7(来源:https://stackoverflow.com/a/28772136/3202193):

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

答案 1 :(得分:0)

这取决于xCode版本。如果您使用的是xCode 6.3或xCode 6.4,那意味着您使用的是Swift 1.2,那么代码必须如下所示:

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    let touch = touches.first as UITouch
    let touchLocation = touch.locationInNode(self)
    let touchedNode = self.nodeAtPoint(touchLocation)
    if(touchedNode.name == "startgame"){
        let gameOverScene = GameScene(size: size)
        gameOverScene.scaleMode = scaleMode
        let transitionType = SKTransition.flipHorizontalWithDuration(1.0)
        view?.presentScene(gameOverScene,transition: transitionType)
    }
}

如果您使用的是xCode 6.2,那意味着您使用的是Swift 1.1,代码应如下所示:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        let touch = touches.first as UITouch
        let touchLocation = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchLocation)
        if(touchedNode.name == "startgame"){
            let gameOverScene = GameScene(size: size)
            gameOverScene.scaleMode = scaleMode
            let transitionType = SKTransition.flipHorizontalWithDuration(1.0)
            view?.presentScene(gameOverScene,transition: transitionType)
        }
    }

请注意,Swift 1.2中引入了Set

希望这会对你有所帮助。 :)