无法分配给酒店:' card'是一个“让...”在swift中的for循环中常量

时间:2018-01-03 11:58:08

标签: swift xcode for-loop

无法分配属性:' card'是一个“让...”在chooseCard方法中循环for循环。我无法弄清楚为什么错误会出现在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
    }


}

1 个答案:

答案 0 :(得分:3)

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