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

iOS - Access the third item in the array and store in a string variable called 'bookTitle'.

Having trouble on the code challenge at "Build a Blog Reader iPhone App > Exploring the Master-Detail Template > Modifying the Master-Detail Template"

Error Message: "Make sure you declare a string called 'bookTitle' and know how do you access the object in an array."

My code:

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

Update: Here's my new code, after some tips from the comments.

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

7 Answers

Wow. I was totally off. I totally read/misunderstood the question. I was trying to change the value of the array at index 2.

This is the statement:

Access the third item in the array and store in a string variable called 'bookTitle'.

How it should be written IMO:

Create a string variable called 'bookTitle' and initialize it to, the third item from the array.

This is the right answer:

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

Make sure you are selecting the right item in the array. In objective-c arrays are zero based so the third item would be 2 not 3.

Hah. Yeah, sorry I realized that fairly quickly after I submitted the question, but it still doesn't pass.

You were asked to declare a string (NSString*), not initialize it as well. Try changing: NSString* bookTitle = @"My Array"; to NSString* bookTitle;

Just changed that too, but still won't pass. Will add an update to my question to reflect the changes I've made.

I don't think you are accessing the array properly. I just did the challenge again and it passed by using objectAtIndex: to access the third item in the array.

Ah. Another occurance of this? http://teamtreehouse.com/forum/access-the-first-fruit-in-your-array

Should have learned my lesson the first time...