将NSUInteger添加到NSMutableArray

dem*_*ro1 3 objective-c nsmutablearray nsuinteger ios

你好我正在研究一个项目,我正在尝试将NSUInteger添加到NSMutableArray.我是Objective-C和C的新手.当我运行应用程序时,NSLog显示为null.

我很感激任何人都能提供的帮助.

这是我的代码

-(NSMutableArray *)flipCardAtIndex:(NSUInteger)index
{
    Card *card = [self cardAtIndex:index];
    [self.flipCardIndexes addObject: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;
    }
    NSLog(@"%@",self.flipCardIndexes[self.flipCardIndexes.count-1]);
    return self.flipCardIndexes;
}
Run Code Online (Sandbox Code Playgroud)

Gab*_*lla 11

NSArray(及其子类NSMutableArray)仅支持对象,不能向其添加本机值.

看看签名 -addObject:

- (void)addObject:(id)anObject
Run Code Online (Sandbox Code Playgroud)

你可以看到它期望id作为参数,它大致意味着任何对象.

所以你必须在NSNumber实例中包装你的整数,如下所示

[self.flipCardIndexes addObject:@(index)];
Run Code Online (Sandbox Code Playgroud)

这里@(index)语法糖[NSNumber numberWithInt:index].

然后,为了将其转换回NSUInteger从阵列中提取它时,您必须按如下方式"展开"它

NSUInteger index = [self.flipCardIndexes[0] integerValue]; // 0 as example
Run Code Online (Sandbox Code Playgroud)