Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add numbers in a mutable array in iPhone?

I am new to iPhone development. I want a Nsmutable array to hold numbers from 1 to 100. How can I do it? How can I implement in a for loop? Is there any other way to hold numbers in array in iPhone?

like image 499
Warrior Avatar asked Apr 10 '10 15:04

Warrior


2 Answers

You can only add NSObject subclasses in Cocoa containers. In your case, you will have to wrap your integers in NSNumber objects:

NSMutableArray *array = [NSMutableArray array];
for( int i = 0; i < 100; ++i )
{
   [array addObject:[NSNumber numberWithInt:i]];
}

To extract the values:

int firstValue = [[array objectAtIndex:0] intValue];
like image 113
Martin Cote Avatar answered Nov 11 '22 10:11

Martin Cote


Use an NSNumber object:

[NSNumber numberWithInt:1];
like image 44
coneybeare Avatar answered Nov 11 '22 10:11

coneybeare