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 Objective-C Basics (Retired) Foundation Framework NSDictionary

Problem

I am trying to do this challenge- by writing the following code: NSDictionary *album = [NSDictionary dictionaryWithObjectsAndKeys:@"Abbey Road",@"title", @"TheBeatles", @"artist", nil];

It does not work. Why?

2 Answers

Aklva,

I do not see an obvious problem with this code except there should be a space in "The Beatles". I tried this code (with space) and it did not work in the code challenge but compiled fine in xcode.

Shouldn't the key come first in the dictionary? I believe it should be

NSDictionary *album = [NSDictionary dictionaryWithObjectsAndKeys:@"title", @"Abbey Road", @"artist", @"The Beatles", nil];

The reason your previous code still compiles in Xcode is because it's valid, but instead of having a key of "title" and a value of "Abbey Road", you'd have a key of "Abbey Road" and a value of "title".

Looks like my .NET is coming through. Your code is valid, mine is not.

This is what the documentation for Xcode writes: NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil];

So the above answer fits the description. I even tested it in Xcode and got correct results after an NSLog of the dictionary: 2014-05-15 06:31:39.138 Constants[35930:303] { Abbey Road" = title; The Beatles" = artist; } Program ended with exit code:0

Unless I am reading the above text wrong, I believe the order of the keys - value pairs is not incorrect. Do you have an idea of what might be wrong. Maybe teamTreehouse does not import the entire NSDictionary.h file?