Firdous
Firdous

Reputation: 4652

Handle NSNumber and NSInteger

Following is a code snippet i am using to add data to nsmutable array, now I am not sure on what to type cast it on while extracting, i need integer value. Problem is that I am getting warnings of 'id' and 'NSInteger' conversion. What could be better way of extracting:

    self.itemsBottom = [NSMutableArray array];
    for (int i = 20; i < 30; i++)
    {
        [itemsBottom addObject:@(i)];
    }

wanna do something like:

NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]

Upvotes: 1

Views: 1981

Answers (2)

Anoop Vaidya
Anoop Vaidya

Reputation: 46543

In this statement

[itemsBottom addObject:@(i)];

you are boxing the integer value to NSNumber.

While here

NSInteger itemAddressed = [self.itemsBottom objectAtIndex:itemIndex]

you are tried to store NSNumber to NSInteger, hence getting the error.

You can use :

NSInteger itemAddressed = [[self.itemsBottom objectAtIndex:itemIndex] integerValue];

Or in short :

NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];

Upvotes: 3

Cocoadelica
Cocoadelica

Reputation: 3026

All seems reasonable...I would think the last line would need to be...

NSInteger itemAddressed = [self.itemsBottom[itemIndex] integerValue];

Maybe?

Upvotes: 2

Related Questions