Reputation: 431
I am building a replica of "Space Invaders" in Swift. The error I am getting:
Use of Undeclared Type 'Set'
The following is a sample of my code:
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)
}
}
and:
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)
}
}
In both cases the error occurs at: (touches: Set<NSObject>, withEvent event: UIEvent)
Can someone please suggest a possible solution?
Upvotes: 2
Views: 4707
Reputation: 23882
I think you are updated your code with new version of swift.
This code was for older versions :
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
}
Set introduced in Swift 1.2
From Xcode 6.3.2 version :
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event)
var touch : UITouch = (touches.first as? UITouch)!
}
From Xcode 7 (Source : 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)
}
Upvotes: 1
Reputation: 1978
Upgrade Xcode to 6.3 or 6.4... Set
was not introduced until Swift 1.2, which came in Xcode 6.3.
That is why Xcode is telling you:
Use of Undeclared Type 'Set'
It does not exist in Xcode 6.2
Upvotes: 0
Reputation: 548
This depends on the xCode version. If you are using xCode 6.3 or xCode 6.4, that means you are on Swift 1.2 then code must be like below:
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)
}
}
If you are on xCode 6.2, that means you are using Swift 1.1 and the code should be like below:
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)
}
}
Please note that Set
is introduced in Swift 1.2.
Hope this will help you. :)
Upvotes: 0