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
Lyric Abbott
35,595 Pointshelp
Remove the last object from your array
my code:
NSMutableArray *elements = [NSMutableArray array];
elements = [NSMutableArray arrayWithObjects:@"Helium", @"Neon", @"Argon", nil]; [elements removeObjectAtIndex:1];
2 Answers
Francisco Navarro
14,185 PointsHi Lyric,
Try this:
[elements removeLastObject];
Rhian Thomas
7,489 PointsWould this work for you?
NSMutableArray *elements = [NSMutableArray array];
elements = [NSMutableArray arrayWithObjects:@"Helium", @"Neon", @"Argon", nil];
if ([elements count] > 0){
[elements removeLastObject];
}
Caleb Kleveter
Treehouse Moderator 37,862 PointsIt appears that the in is missing in your ifin statement, am I wrong?
Rhian Thomas
7,489 PointsThe if statement is just making sure that the count of objects in elements is greater than 0 before removing the last object.
Therefore I do not believe that an if in is necessary. However, I have not done much Objective C coding recently as I am focusing on Swift... so I may be wrong ;)
The code above does compile and behave as expected in my quick tests.
Caleb Kleveter
Treehouse Moderator 37,862 PointsOkay, good.