Question

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]
Was it helpful?

Solution

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];

OTHER TIPS

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

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

Maybe?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top