不能赋值'colorTouched'是'let'常量

迅速的初学者,按照书并得到错误“”

这里是代码:

@IBAction func buttonTouched(sender : UIButton) { var buttonTag : Int = sender.tag if let colorTouched = ButtonColor(rawValue: buttonTag) { if currentPlayer == .Computer { return } //error here: if colorTouched = inputs[indexOfNextButtonToTouch] { indexOfNextButtonToTouch += 1 if indexOfNextButtonToTouch == inputs.count { // 玩家成功地完成了这一轮 if advanceGame() == false { playerWins() } indexOfNextButtonToTouch = 0 } else { } } else { playerLoses() indexOfNextButtonToTouch = 0 } } } 

所以如果我不能使用“如果让colorTouched”,我该怎么办呢?

你应该使用==比较,而不是= (这是分配):

 @IBAction func buttonTouched(sender : UIButton) { var buttonTag : Int = sender.tag if let colorTouched = ButtonColor(rawValue: buttonTag) { if currentPlayer == .Computer { return } // note double equals if colorTouched == inputs[indexOfNextButtonToTouch] { ... 
Interesting Posts