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 Exploring the Master-Detail Template Modifying the Master-Detail Template

Justin Cheng
Justin Cheng
3,804 Points

Challenge: Create a string variable called 'bookTitle' and assign it the third item from the 'booksArray'.

I don't understand what is wrong with my code. I initialized the array with the names of the books. Next, I create a string variable and assign it the value of the third item in my array. I obtain this item by sending a message to my array and tell it to give me the object at index 2 (which should technically be the third one). Help please!

\ code

NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];

NSString *bookTitle = [self.booksArray objectAtIndex:2];

\

4 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Justin,

Try excluding the self reference when accessing the third element from the array. Like this.

NSString *bookTitle = [booksArray objectAtIndex:2];

I hope this helps.

First you need to alloc and then initialize with objects.

NSArray *booksArray = [[NSArray alloc] initWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];

Below, you are assigning the object at that index to a bookTitle and I think thats fine but you will have a warning of "unused variable "bookTitle".

NSString *bookTitle = [booksArray objectAtIndex:2];

I hope I was able to help you.

Michael Schutz
Michael Schutz
1,615 Points

I just did this challenge, and figured I should remove the "self" reference as well. But doesn't that make this challenge work against the tutorial? In the actual code we work with, there needs to be a "self" reference in the app. It's always there in the videos. So we're being quizzed on something that's never actually presented.

Is there a way that you can tweak this challenge to keep the "self" in the code, for the sake of consistency?

Justin Cheng
Justin Cheng
3,804 Points

Thank you guys! Removing the "self" worked.