无法分配给值'colorTouched'是'let'常量 [英] cannot assign to value 'colorTouched' is a 'let' constant

查看:109
本文介绍了无法分配给值'colorTouched'是'let'常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

迅速入门,请按照书中的内容进行操作,并显示错误"

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

下面是代码:

@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",那该怎么办?

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

推荐答案

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

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

这篇关于无法分配给值'colorTouched'是'let'常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆