我一直在尝试为一副纸牌编写一个应用程序,但是我的代码中不断出现错误。错误在otherCard
上并说
#define MATCH_BONUS 4
#define MISMATCH_PENALTY 2
#define FLIP_COST 1
- (void)flipCardAtIndex:(NSUInteger)index
{
card *card = [self cardAtIndex:index];
if (!card.isUnplayable){
if(!card.isFaceUp){
for (card *otherCard in self.cards) {
if (otherCard.isFaceUp && !otherCard.isUnplayable) {
int matchscore = [card match: @[otherCard]];
if (matchscore) {
otherCard.unplayable = YES;
card.unplayable = YES;
self.score += matchscore * MATCH_BONUS;
} else {
otherCard.faceUp = NO;
self.score -= MISMATCH_PENALTY;
}
break;
}
}
self.score -= FLIP_COST;
}
card.faceUp = !card.isFaceUp;
}
}
最佳答案
您的card
类被card
变量掩盖了。
card *card = [self cardAtIndex:index];
这意味着for每个循环在这里失败:
for (card *otherCard in self.cards) {
尝试将大写的
card
类更改为Card
(大写的类名也是很好的样式)。或者,您可以将card
变量重命名为flipCard
。