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 trialNkem Modu
4,511 PointsAccessing an object in an array
Task 1 asked me to create the following array, which I did:
NSArray *booksArray = [NSArray arrayWithObjects: @"Hamlet", @"King Lear", @"Othello", "Macbeth",nil];
Task 2: asks me to "Create a string variable called 'bookTitle' and assign it the third item from the 'booksArray'". So I tried:
NSArray *booksArray = [NSArray arrayWithObjects: @"Hamlet", @"King Lear", @"Othello", "Macbeth",nil]; //line from task 1
NSString *bookTitle = booksArray[2];
I get a "Bummer! Try again!" error, so I replace the second line with:
NSString *bookTitle = [booksArray objectAtIndex:2];
Still getting the "Bummer! Try again!" error. If I change anything else though, I get an error which reads "Make sure you declare a string called 'bookTitle' and object in the array using the method 'objectAtIndex'."
3 Answers
Stone Preston
42,016 PointsActually your code should not have worked. You forgot the @ on macbeth
NSArray *booksArray = [NSArray arrayWithObjects: @"Hamlet", @"King Lear", @"Othello", "Macbeth",nil];
looks like thats a bug in the challenge engine if it let that through on the first task. Amit Bijlani can you look into this bug? I took the challenge again and it does let that syntax error slip through on the first task, and then catches it on the second task.
Stone Preston
42,016 Pointsyour code looks like it should have worked. I just passed with:
NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];
NSString *bookTitle = [booksArray objectAtIndex:2];
Nkem Modu
4,511 PointsHa thanks man.
Just because a line of code passes task #1 doesn't guarantee that the same line will pass task #2. Lesson learned.
Stone Preston
42,016 Pointswell normally it does. The engine should not have let that through with that syntax error.
Amit Bijlani
Treehouse Guest TeacherAmit Bijlani
Treehouse Guest TeacherThanks Stone Preston. I will take a look.