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

Guess Not Working.

Hi! i just tried two ways to initialize the dictionary, the alloc init way and the direct way, but none of them works! so what is te correct way to do that?

This is the first way i tried:

NSDictionary * album = [[NSDictionary alloc] initWithObjectsAndKeys:@"Abby Road",@"album",@"The Beatles",@"artist", nil];

And this is the other:

NSDictionary *album = @{@"album":@"Abbey Road", @"artist":@"The Beatles"};

But still sending me wrong, so i'm stuck.

2 Answers

James Carney
James Carney
16,255 Points

What code challenge are you working on? Try this though...

NSDictionary *album = [[NSDictionary alloc] initWithObjectsAndKeys: @"Abby Road", @"album", @"The Beatles" , @"artist", nil];

Another way you could do it is like this:

- (instancetype)initWithObjects:(NSArray *)objects forKeys:(NSArray *)keys

"Initializes a newly allocated dictionary with entries constructed from the contents of the objects and keys arrays." description of the second example.

Hi thanks for the replay I did the challenge of NSDictionary but the way I solved it was with NSDictionary * albumMutable = [NSDictionary dictionaryWithDictionary: album]; thanks for the help!!!!