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 trialEthan Parker
494 PointsCode 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
Kenneth John Balgos
657 PointsNSArray *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
2,118 PointsNSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];
NSString *bookTitle = booksArray[2];
Kenneth John Balgos
657 PointsI 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.
Ethan Parker
494 PointsNSArray *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
6,115 PointsNSString *bookTitle = [booksArray objectAtIndex:2]; // Not 3, cause you count from 0, so the third one is number 2.
Kenneth John Balgos
657 PointsI believe you need to complete the "Build a Simple iPhone App" project to understand this well. You should not skip any topics. :)
Joseph Finkenbinder
5,340 PointsBuild 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?
Taylor Hammons
1,609 Pointssyntax is wrong, I did the same thing. It's "objectAtIndex" not "objectsAtIndex" no "s." Everything else looks right
Joel Bell
8,586 PointsJoseph 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.
Joseph Finkenbinder
5,340 PointsThanks Joel Bell!
Chris Liu
7,953 PointsI was under the impression Joseph was using a convenience constructor, which shortens the code while still initiating the object?
Sterling Calder
2,341 PointsI also thought we were able to make use of the convenience constructor? Why is this not the case?
elieschoppik
16,886 PointsJust 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];
Taylor Hammons
1,609 PointsMight 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
9,809 PointsI got mine to work with the convenience constructor:
NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];
Esma Goktekin
3,870 PointsNon 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
3,870 PointsIt worked! NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet",@"King Lear",@"Othello",@"Macbeth",nil]; NSString *bookTitle = booksArray[2];