1

How can you make this work?

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

Thanks :)

flag

5  
Ha! You a Javascript/Ruby/Scheme/ML programmer by any chance? – quixoto Feb 9 at 17:36
Yep :) Hehe, used to the other ways of doing things, Objective-C is unusual to me.. :P – Emil Feb 9 at 17:49

3 Answers

5
NSMutableArray * array = [[NSMutableArray alloc] init];

for (int i = 0; i <= 60; ++i) {
  [array addObject:[NSNumber numberWithInt:i]];
}
link|flag
2
int myStrangeNumberOfItems = 61;

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

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

Your Answer

get an OpenID
or
never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.