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 Build a Blog Reader iPhone App Getting Data from the Web Mutable vs Immutable

Not able to get through the challenge.

What's wrong in my code?

mutablearray.mm
NSMutableArray *elements=[NSMutableArray array];
[elements addObject:@"Helium"];
[elements addObject:@"Neon"];
[elements addObject:@"Argon"];
[elements removeObject:@"Neon"];
[elements removeObjectAtIndex:2];

4 Answers

Got the answer. The syntax is [elements removeLastObject]; . Thank you guys for the help.

Ahhhhh thank you!!

You only have two objects in your array (after deleting "Neon"), and are trying to delete object number three, which doesn't exist. Remember that the index starts at 0 in an array, so index 2 would be the third object.

I agree with Eirik, you're accessing an index that has been deleted. At the point that the final line executes, you should only have 0 and 1 as available indices.

I have changed it to [elements removeObjectAtIndex:1] and even then it does not work . it says a method must be called on the "elements" array to remove the last object.

The new code is NSMutableArray *elements=[NSMutableArray array]; [elements addObject:@"Helium"]; [elements addObject:@"Neon"]; [elements addObject:@"Argon"]; [elements removeObject:@"Neon"]; [elements removeObjectAtIndex:1];