将NSUInteger添加到NSMutableArray [英] Add NSUInteger to NSMutableArray

查看:120
本文介绍了将NSUInteger添加到NSMutableArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在处理一个项目,我正在尝试将NSUInteger添加到NSMutableArray。我是Objective-C和C的新手。当我运行应用程序时,NSLog显示为空。

Hello I am working on a project and I am trying to add an NSUInteger to an NSMutableArray. I am new to Objective-C and C in general. When I run the app NSLog displays null.

我很感激任何人都可以提供帮助。

I'd appreciate any help anyone is able to provide.

这是我的代码

-(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 )仅支持对象,不能为其添加原始值。

NSArray (along with its subclass NSMutableArray) only supports objects, you cannot add native values to it.

查看 -addObject:

- (void)addObject:(id)anObject

正如你所看到的期望 id 作为参数,大致意味着任何对象

As you can see it expects id as argument, which roughly means any object.

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

So you have to wrap your integer in a NSNumber instance as follows

[self.flipCardIndexes addObject:@(index)];

其中 @(索引) [NSNumber numberWithInt:index] 的href =http://clang.llvm.org/docs/ObjectiveCLiterals.html#nsnumber-literals>语法糖

where @(index) is syntactic sugar for [NSNumber numberWithInt:index].

然后,为了从数组中提取它,将其转换回 NSUInteger ,你必须打开它如下

Then, in order to convert it back to NSUInteger when extracting it from the array, you have to "unwrap" it as follows

NSUInteger index = [self.flipCardIndexes[0] integerValue]; // 0 as example

这篇关于将NSUInteger添加到NSMutableArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆