Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

cameryn smith
cameryn smith
2,828 Points

How do you add strings to a declared NSMutableArray?

Task 2

Add the following strings to the ‘elements’ array: 'Helium','Neon', 'Argon'

I'm lossed -____- LOL

NSMutableArray *elements = [NSMutableArray array];

//Now here is where I get stuck

[book1 addObject:@"book2",@"book3",@"book4"];

2 Answers

Nick Sint
Nick Sint
4,208 Points

The solution to your problem is quite simple.

You created an array called elements therefore this is the array you want to add all your objects in.

Note, the method you called addObject: is literally what it says, you add an object to the array. Specifically 1 object at a time. So if you want to add 3 objects, you need to call it 3 times.

So you have 2 choices if you want to create an array called elements with 3 objects ('Helium','Neon', 'Argon').

Option 1:

    NSMutableArray *elements = [[NSMutableArray alloc] initWithObjects:@"Helium",@"Neon",@"Argon",nil];

Option 2:

    NSMutableArray *elements = [NSMutableArray array];
    [elements addObject:@"Helium"];
    [elements addObject:@"Neon"];
    [elements addObject:@"Argon"];
cameryn smith
cameryn smith
2,828 Points

Thx I remember now :P