Add NSUInteger to NSMutableArray

2020-02-29 10:38发布

问题:

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.

Here is my code

-(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;
}

回答1:

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

Check out the signature of -addObject:

- (void)addObject:(id)anObject

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

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

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

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

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


回答2:

You can only add objects to NSMutableArrays. The addObject accepts objects of type id, which means it will accept an object.

NSIntegers and NSUIntegers, however, are not objects. They are just defined to be C style variables.

#if __LP64__ || NS_BUILD_32_LIKE_64
    typedef long NSInteger;
    typedef unsigned long NSUInteger;
#else
    typedef int NSInteger;
    typedef unsigned int NSUInteger;
#endif

As you can see, they are just defined to be ints and longs based on a typedef macro.

To add this to your array, you need to first convert it to an object. NSNumber is the Objective C class that allows you to store a number of any type. To make the NSNumber, you will want to you the numberWithInt method, passing your variable as the parameter.

NSNumber *number = [NSNumber numberWithInt:card];

Now that your variable is wrapped in an object, you can add it to the array.

[self.flipCardIndexes addObject:number];

Finally, if you want to retrieve the element at a future time, you have to remove the object and then convert it back to an int value you can use. Call

NSNumber *number = [self.flipCardIndexes objectAtIndex:index];

Where index is the index of the card you are trying to retrieve. Next, you have to convert this value to an integer by calling integerValue.

NSUInteger *value = [number integerValue];