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 Getting Data from the Web What is NSDictionary?

Question concerning Blog Reader NSArray

Hello, I have a question concerning the final step in this process. I have declared NSDictionay entries for books1 and books 2, but am having problems with the array called 'booksArray' that pulls the items from those dictionaries in:

NSDictionary *book1 = [NSDictionary dictionaryWithObjectsAndKeys:@"Art of War",@"Title", @"Sun Tzu", @"Author", nil]; NSDictionary *book2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Brave New World",@"Title", @"Aldous Huxley", @"Author", nil];

NSArray *booksArray= [NSArray arrayWithObjects: books1, books2, nil];

What am I doing wrong here?

Thanks in advance, -R

Patrick Cooney
Patrick Cooney
12,216 Points

What is the error you're getting?

4 Answers

Robert Bojor
PLUS
Robert Bojor
Courses Plus Student 29,439 Points

Your NSArray objects are wrong, they contain an extra "s" at the end...

NSArray *booksArray= [NSArray arrayWithObjects: book1, book2, nil];

It's an easy mistake to make when typing vars, especially if you don't have code validation in the challenge.

Peter Pult
Peter Pult
8,095 Points

Also you might want to take a look at literals in Obj-C, which really will clean up your code.

NSDictionary *book1 = @{
   @"Title" : @"Art of War",
   @"Author" : @"Sun Tzu"
}; 
NSDictionary *book2 = @{
   @"Title" : @"Brave New World",
   @"Author" : @"Aldous Huxley"
};

NSArray *booksArray= @[book1, book2];

More information about literals can be found here.

EDIT: I'm not sure if these are allowed in the code challenge, but there should be no difference at runtime. ;-)

Brilliant - thanks for posting this.

I had issues with this today as well. I had typed

NSDictionary *book1 = [NSDictionary DictionaryWithObjectsAndKeys:@"Art of War",@"Title", @"Sun Tzu", @"Author", nil]

and this was not accepted to pass the first part of the quiz.

So I just copied what Peter Pult suggested here ie using literals, and that worked - but this is not something we have seen in the videos.

Can anyone explain why my code was not accepted? I did it as per I ha learned in the video... frustrating as hell!

Peter Pult
Peter Pult
8,095 Points

Take a look at your method DictionaryWithObjectsAndKeys you capitalized the first letter. Methods (should) always start with a small letter, so like this dictionaryWithObjectsAndKeys.

Also you are missing the semicolon at the end of the line.

Thanks Peter. In the code challenge I had included the semicolon (I just typed it up quickly here to ask the question).

But I did make the mistake with the capital letter on the D...

thanks again, you are a star!