0

我一直在尝试为一副纸牌编写应用程序,但我的代码中不断出现错误。错误开启otherCard并说

未声明的标识符 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;
    }
}
4

1 回答 1

1

您的card班级被card变量掩盖了。

card *card = [self cardAtIndex:index];

这意味着 for each 循环在这里失败:

for (card *otherCard in self.cards) {

尝试将您的card班级更改Card为大写 C(大写班级名称也是一种不错的风格)。或者,您可以将card变量重命名为flipCard.

于 2013-07-18T19:55:46.907 回答