cannot assign to value 'colorTouched' is a 'let' constant [closed]

牧云@^-^@ 提交于 2019-12-13 09:24:50

问题


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:


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


来源:https://stackoverflow.com/questions/37399828/cannot-assign-to-value-colortouched-is-a-let-constant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!