无法分配属性:'card'是swift中for循环的'let'常量[duplicate]

问题描述 投票:4回答:1

这个问题在这里已有答案:

无法分配属性:'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
    }


}
swift xcode for-loop
1个回答
3
投票

卡片当然是一个让它不变,使它变量使用:for var card in cards

© www.soinside.com 2019 - 2024. All rights reserved.