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

Code Challenge in Modifying the Master-Detail Template - Help!

I got the first part, here's my array:

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

Then it says "Access the third item in the array and store in a string variable called 'bookTitle'."

Sorry, I really don't know what it means by "Access" the item. Here's my best guess:

NSString *bookTitle = @"Othello"; //or
NSString *bookTitle = 3; //or something, I don't know

11 Answers

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

The objectAtIndex is a method of an array object and index is 2 because the index counting starts from 0.

Daniel Jurado
Daniel Jurado
2,118 Points

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

NSString *bookTitle = booksArray[2];

I think that means you have to save the 3rd string from your *booksArray to a new variable named *bookTitle. Tell me if I have to write the answer.

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

NSString *bookTitle = self.booksArray 3;

This is my best guess, yeah little help?

Emmanuel Darmon
Emmanuel Darmon
6,115 Points

NSString *bookTitle = [booksArray objectAtIndex:2]; // Not 3, cause you count from 0, so the third one is number 2.

I believe you need to complete the "Build a Simple iPhone App" project to understand this well. You should not skip any topics. :)

Build a Blog Reader iPhone App > Exploring the Master-Detail Template > Modifying the Master-Detail Template Code Challenge 2 of 3.

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

My code: NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil]; NSString *bookTitle = [booksArray objectsAtIndex:2];

Error response: Bummer! Make sure you declare a string called 'bookTitle' and object in the array using the method 'objectAtIndex'.

What am I doing wrong?

syntax is wrong, I did the same thing. It's "objectAtIndex" not "objectsAtIndex" no "s." Everything else looks right

Joseph Finkenbinder,

You haven't allocated and initiated the object.

Look at the first post where he allocates the object using [NSArray alloc]. After that you can initWithObjects:@"Hamlet", etc.

Hope this helps.

Thanks Joel Bell!

Chris Liu
Chris Liu
7,953 Points

I was under the impression Joseph was using a convenience constructor, which shortens the code while still initiating the object?

I also thought we were able to make use of the convenience constructor? Why is this not the case?

Just tried submitting this for the second part - and it just says "Bummer, Try Again!" Can anyone help?

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

NSString *bookTitle = [booksArray objectAtIndex:2];

Might be something wrong with the source code b/c that's exactly what I have and it worked for me. Only difference is I put a space between the colon and 2 --> objectAtIndex: 2];

Anthony DeFreitas
Anthony DeFreitas
9,809 Points

I got mine to work with the convenience constructor:

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

Esma Goktekin
Esma Goktekin
3,870 Points

Non of the comments above works for me. I don't know what is wrong.

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

Esma Goktekin
Esma Goktekin
3,870 Points

It worked! NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet",@"King Lear",@"Othello",@"Macbeth",nil]; NSString *bookTitle = booksArray[2];