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.

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];