ios - 将 NSUInteger 添加到 NSMutableArray

标签 ios objective-c nsmutablearray nsuinteger

你好,我正在做一个项目,我正在尝试将 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/

相关文章:

iphone - 将UIView中的UIImageView对象添加到NSMutableArray中

ios Firebase Dynamic Link 不打开已安装的应用程序

ios - CBCentralManager 未开机

ios - 从用户位置查找数组中最接近的经度和纬度,并根据从一个纬度到另一个纬度的最短距离对数组进行排序

如果无法使用 NSURLSession 下载文件,iOS::error 为零

objective-c - 获取AppKit类的私有(private)方法的名称

ios - 在表格单元格中添加到 UIView 的图形在滚动表格时表现得很奇怪

ios - Objective-C 中的多维 NSMutableArrays

ios - NSMutableArray 属性初始化和更新

ios - MPMoviePlayerController 看到双重