1

How can you make this work?

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

Thanks :)

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

3 Answers 3

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

for (int i = 0; i <= 60; ++i) {
  [array addObject:[NSNumber numberWithInt:i]];
}
Sign up to request clarification or add additional context in comments.

Comments

2
int myStrangeNumberOfItems = 61;

NSMutableArray * numbers = [[NSMutableArray alloc] initWithCapacity: myStrangeNumberOfItems];
for (int i = 0; i < myStrangeNumberOfItems; i++) {
    [numbers addObject:[NSNumber numberWithInt:i]];
}

2 Comments

that is totally unacceptable.. it can make a spaceship go nuts
Fixed. ;) Never loop to <= if you don't need to.
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]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.