你好,我正在做一个项目,我正在尝试将 NSUInteger 添加到 NSMutableArray。一般来说,我是 Objective-C 和 C 的新手。当我运行应用程序时,NSLog 显示为空。
如果有人能提供任何帮助,我将不胜感激。
这是我的代码
-(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;
}
最佳答案
NSArray
(及其子类 NSMutableArray
)仅支持对象,不能向其添加 native 值。
查看 -addObject:
的签名
- (void)addObject:(id)anObject
如您所见,它需要 id
作为参数,这大致意味着任何对象。
所以你必须将整数包装在 NSNumber
实例中,如下所示
[self.flipCardIndexes addObject:@(index)];
其中 @(index)
是 syntactic sugar对于 [NSNumber numberWithInt:index]
。
然后,为了在从数组中提取它时将其转换回NSUInteger
,您必须按如下方式“展开”它
NSUInteger index = [self.flipCardIndexes[0] integerValue]; // 0 as example
关于ios - 将 NSUInteger 添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422035/