Ios Swift错误-使用未声明的类型

Ios Swift错误-使用未声明的类型,ios,swift,Ios,Swift,我正在用Swift制作一个“太空入侵者”的复制品。我得到的错误是: 使用未声明的类型“Set” 以下是我的代码示例: override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { let touch = touches.first as UITouch let touchLocation = touch.locationInNode(self) let touche

我正在用Swift制作一个“太空入侵者”的复制品。我得到的错误是:

使用未声明的类型“Set”

以下是我的代码示例:

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 touchsbegined(touchs:Set,withEvent-event:UIEvent){
let touch=触摸。首先作为UITouch
让touchLocation=touch.locationInNode(自身)
让touchedNode=self.nodeAtPoint(touchLocation)
if(touchedNode.name==“startName”){
let gameOverScene=游戏场景(大小:大小)
gameOverScene.scaleMode=scaleMode
让transitionType=SKTransition.flipHorizontalWithDuration(1.0)
视图?.presentScene(游戏场景,过渡:过渡类型)
}
}
以及:

override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
/*当触摸开始时调用*/
用于触摸输入(触摸设置){
let location=touch.locationInNode(自)
让sprite=SKSpriteNode(图像名为:“太空船”)
sprite.xScale=0.5
sprite.yScale=0.5
sprite.position=位置
let action=SKAction.rotateByAngle(CGFloat(M_PI),持续时间:1)
sprite.runAction(SKAction.repeatActionForever(action))
self.addChild(精灵)
}
}
在这两种情况下,错误都发生在:
(touch:Set,withEvent:UIEvent)


有人能提出一个可能的解决方案吗

我想您的代码已经用新版本的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)!

}
override func touchsbegined(touchs:Set,withEvent-event:UIEvent){
super.touchsbegind(touchs,withEvent:event)
变量touch:UITouch=(touch.first as?UITouch)!
}
从Xcode 7(源代码:):

覆盖函数触摸开始(触摸:设置,withEvent事件:UIEvent?){
如果让触摸=先触摸{
// ...
}
super.touchsbegind(touchs,withEvent:event)
}

这取决于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)
    }
}
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


希望这对您有所帮助。:)

将Xcode升级到6.3或6.4<代码>集合直到Swift 1.2才引入,它出现在Xcode 6.3中

这就是为什么Xcode告诉您:

使用未声明的类型“Set”


它在Xcode 6.2中不存在

您使用的Xcode的具体版本是什么?版本6.2(6C131e)将Xcode升级到6.3或6.4<代码>集合直到Swift 1.2才引入,它出现在Xcode 6.3中。
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: 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)
        }
    }