I am building a replica of "Space Invaders" in Swift. The error I am getting:
我正在建造一个“太空入侵者”的复制品。我得到的错误是:
Use of Undeclared Type 'Set'
使用未声明类型'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)
在这两种情况下,错误都发生在:(触摸:Set
Can someone please suggest a possible solution?
谁能提出一个可能的解决办法吗?
3 个解决方案
#1
1
I think you are updated your code with new version of swift.
我认为你更新了你的代码,新版本的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
设置在Swift 1.2中。
From Xcode 6.3.2 version :
从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)!
}
From Xcode 7 (Source : https://*.com/a/28772136/3202193):
来自Xcode 7(来源:https://*.com/a/28772136/3202193):
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}
#2
0
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:
这取决于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)
}
}
If you are on xCode 6.2, that means you are using Swift 1.1 and the code should be like below:
如果您使用的是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)
}
}
Please note that Set
is introduced in Swift 1.2.
请注意,Set是在Swift 1.2中引入的。
Hope this will help you. :)
希望这能对你有所帮助。:)
#3
0
Upgrade Xcode to 6.3 or 6.4... Set
was not introduced until Swift 1.2, which came in Xcode 6.3.
将Xcode升级到6.3或6.4…在Xcode 6.3中出现的Swift 1.2之前没有引入Set。
That is why Xcode is telling you:
这就是为什么Xcode告诉你:
Use of Undeclared Type 'Set'
使用未声明类型'Set'
It does not exist in Xcode 6.2
它在Xcode 6.2中不存在
#1
1
I think you are updated your code with new version of swift.
我认为你更新了你的代码,新版本的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
设置在Swift 1.2中。
From Xcode 6.3.2 version :
从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)!
}
From Xcode 7 (Source : https://*.com/a/28772136/3202193):
来自Xcode 7(来源:https://*.com/a/28772136/3202193):
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let touch = touches.first {
// ...
}
super.touchesBegan(touches, withEvent:event)
}
#2
0
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:
这取决于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)
}
}
If you are on xCode 6.2, that means you are using Swift 1.1 and the code should be like below:
如果您使用的是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)
}
}
Please note that Set
is introduced in Swift 1.2.
请注意,Set是在Swift 1.2中引入的。
Hope this will help you. :)
希望这能对你有所帮助。:)
#3
0
Upgrade Xcode to 6.3 or 6.4... Set
was not introduced until Swift 1.2, which came in Xcode 6.3.
将Xcode升级到6.3或6.4…在Xcode 6.3中出现的Swift 1.2之前没有引入Set。
That is why Xcode is telling you:
这就是为什么Xcode告诉你:
Use of Undeclared Type 'Set'
使用未声明类型'Set'
It does not exist in Xcode 6.2
它在Xcode 6.2中不存在