views:

359

answers:

3

How can you make this work?

numbers = [[NSMutableArray alloc] initWithObjects: ({int x = 0; while (x <= 60 ) { return x; x++; } })];

Thanks :)

+5  A: 
NSMutableArray * array = [[NSMutableArray alloc] init];

for (int i = 0; i <= 60; ++i) {
  [array addObject:[NSNumber numberWithInt:i]];
}
Dave DeLong
Thank you :) --
Emil
+2  A: 
int myStrangeNumberOfItems = 61;

NSMutableArray * numbers = [[NSMutableArray alloc] initWithCapacity: myStrangeNumberOfItems];
for (int i = 0; i < myStrangeNumberOfItems; i++) {
    [numbers addObject:[NSNumber numberWithInt:i]];
}
quixoto
Off-by-one error.
Matthew Flaschen
that is totally unacceptable.. it can make a spaceship go nuts
Anurag
Fixed. ;) Never loop to <= if you don't need to.
quixoto
+2  A: 

First, an NSArray can only hold objects, not primitives. You can add the objects within a for loop like so.

NSMutableAray * numbers = [[NSMutableArray alloc] init];
for (int x = 0; x <= 60; x++)
    [numbers addObject:[NSNumber numberForInt:x]];
Cory Kilger