Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add NSUInteger to NSMutableArray

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;
}
like image 352
demuro1 Avatar asked Dec 07 '25 03:12

demuro1


1 Answers

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
like image 148
Gabriele Petronella Avatar answered Dec 08 '25 17:12

Gabriele Petronella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!