If you want to make an array of integers, can you use NSInteger? Do you have to use NSNumber? If so, then why?
See Question&Answers more detail:osIf you want to make an array of integers, can you use NSInteger? Do you have to use NSNumber? If so, then why?
See Question&Answers more detail:osYou can use a plain old C array:
NSInteger myIntegers[40];
for (NSInteger i = 0; i < 40; i++)
myIntegers[i] = i;
// to get one of them
NSLog (@"The 4th integer is: %d", myIntegers[3]);
Or, you can use an NSArray
or NSMutableArray
, but here you will need to wrap up each integer inside an NSNumber
instance (because NSArray
objects are designed to hold class instances).
NSMutableArray *myIntegers = [NSMutableArray array];
for (NSInteger i = 0; i < 40; i++)
[myIntegers addObject:[NSNumber numberWithInteger:i]];
// to get one of them
NSLog (@"The 4th integer is: %@", [myIntegers objectAtIndex:3]);
// or
NSLog (@"The 4th integer is: %d", [[myIntegers objectAtIndex:3] integerValue]);