无法分配给值 'colorTouched' 是一个 'let' 常量

cannot assign to value 'colorTouched' is a 'let' constant

初学者swift,照着书做,得到错误“”

代码如下:

@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
        }
    }
}

所以如果我不能使用 "if let 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] {
    ...