Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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