cannot assign to value 'colorTouched' is a

2019-09-22 23:08发布

beginner of swift, follow the book and get the error ""

here is the code:

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

so if I cannot use "if let colorTouched", what should I do with it?

1条回答
男人必须洒脱
2楼-- · 2019-09-22 23:45

You should be using == for comparison instead of = (which is assignment):

@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] {
    ...
查看更多
登录 后发表回答