无法赋值'colorTouched'是'let'常量

时间:2016-05-23 20:20:12

标签: ios swift uibutton sender

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”,我该怎么办呢?

1 个答案:

答案 0 :(得分:4)

您应该使用==进行比较而不是=(这是作业):

@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] {
    ...