xcode 无法分配给属性:'card' 是 swift for 循环中的 'let' 常量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48076937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:30:20  来源:igfitidea点击:

Cannot assign to property: 'card' is a 'let' constant in a for loop in swift

swiftxcodefor-loop

提问by sriram hegde

Cannot assign to property: 'card' is a 'let' constant in for loop in chooseCard method. I am not able to figure out why the error is coming in the for loop .what am I missing in the code .Please help.

无法分配给属性:'card' 是chooseCard 方法中 for 循环中的一个 'let' 常量。我无法弄清楚为什么错误会出现在 for 循环中。我在代码中遗漏了什么。请帮忙。

import Foundation

class Concentration {

    var cards = [Card]()

    var faceUpCount = 0

    func chooseCard(atIndex : Int) {
        var alreadyFaceUpCard  = Card()
        if faceUpCount == 0 {
            alreadyFaceUpCard = cards[atIndex]
            alreadyFaceUpCard.isFaceUp = true
            faceUpCount += 1
        }else if faceUpCount == 1 {
            var card = cards[atIndex]
            card.isFaceUp = true
            if alreadyFaceUpCard.identifier == card.identifier {
                alreadyFaceUpCard.isMatchUp = true
                card.isMatchUp = true
                faceUpCount += 1
            }

        }else {
            for card in cards {
                card.isFaceUp = false
            }

        }

    }



    init(numberOfPairsOfCards : Int) {
        for _ in 1...numberOfPairsOfCards {
            let card = Card()
            cards.append(card)
            cards.append(card)
        }
        // TODO: Shuffle Cards
    }


}

回答by Madhur

card is ofcourse a let constant, to make it variable use: for var card in cards

卡当然是一个让常量,使其可变使用: for var card in cards