我试图在 switch 语句之外定义一个常量,以便在 switch 语句执行完毕后使用它并在 switch 语句中分配它:
let action: SKAction!
switch (whatever) {
case 0:
sprite.position = CGPointMake(0, self.scene.size.height * lengthDiceroll)
action = SKAction.moveTo(CGPointMake(self.scene.size.width, self.scene.size.height * (1 - lengthDiceroll)), duration: 1) // error here
// other actions
default:
println("meh")
// add the sprite to the scene
let sequence = SKAction.sequence([action, action2])
sprite.runAction(SKAction.repeatActionForever(sequence))
self.addChild(sprite)
但我收到错误无法分配给“let”值“action”
。如何分配给操作以便我可以在 switch 之外使用它?
如果我尝试:
action! = SKAction.moveTo(CGPointMake(self.scene.size.width, self.scene.size.height * (1.0 - lengthDiceroll)), duration: 1)
我收到错误 “找不到接受提供的参数的 '*' 的重载”
。
最佳答案
用 let
声明的变量必须立即赋值。您不能像在第一行尝试做的那样简单地使用 let 定义一个没有值的变量。
你说你想让 action 成为常量,但是你在运行时修改它的值,这不是常量。因此,您需要一个变量,即使它的值只改变一次。
关于switch-statement - 无法分配给 'let' 值 'action',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24225189/