-1

ViewController.h

@property (strong, nonatomic) NSMutableArray *numbers;

ViewController.m

- (void)viewDidLoad
{

    [super viewDidLoad];

    for (int counter = 0; counter < 10; counter++) {

        [numbers addObject:counter];

    }

}

I tried to fill up the array numbers with a for loop counting from 0 - 9 but I don't know how to do so. Any help would be appreciated!

0

1 Answer 1

3

NSArray cannot contain primitives, you must add objects to it.

Objects that correspond to integers are NSNumbers. You can make them using the old syntax, like this,

[numbers addObject:[NSNumber numberWithInt:counter]];

or using the new syntax, like this:

[numbers addObject:@(counter)];

It goes without saying that before adding objects to the array you need to create it:

numbers = [NSMutableArray array];
Sign up to request clarification or add additional context in comments.

2 Comments

And, of course, before you can even use the array you have to create it.
@HotLicks Thanks for mentioning it, forgetting to create an array is indeed a rather common omission.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.