这个问题在这里已有答案:
无法分配属性:'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
}
}
卡片当然是一个让它不变,使它变量使用:for var card in cards