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

thanh trung
thanh trung
4,246 Points

having problem with NSDictionary

<code> NSDictionary *ablbum = [[NSDictionary alloc] initWithObjectsAndKeys:@"title", @"Abbey", @"artist", @"The Beatles", nil ]; </code>

thanh trung
thanh trung
4,246 Points

whats wrong with the code?

5 Answers

Patrick Donahue
Patrick Donahue
9,523 Points

Can you post the code that you have? I am not sure what the issue is. I put the code in my Xcode and ran it and got it to load in the console.

NSDictionary *album = [[NSDictionary alloc] initWithObjectsAndKeys:@"title", @"Abbey", @"artists", @"The Beatles", nil]; NSLog(@"%@", album);

2014-03-18 09:33:13.269 ] { Abbey = title; "The Beatles" = artists; }

Scott Evans
Scott Evans
4,236 Points

Hey Thanh, please see the code below.

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

To explain, The 2 things that were wrong were you were declaring the Key / Pairs in the wrong order. When using initWithObjectsAndKeys you declare them like @"VALUE",@"KEY",@"VALUE",@"KEY" The only other thing was you put @"Abbey" instead of @"Abbey Road".

Hope this helps

Patrick Donahue
Patrick Donahue
9,523 Points

I didn't even realize they were in the wrong order. :-(

Scott Evans
Scott Evans
4,236 Points

It took me a minute to realise, but then i looked at your console output Abbey = title then the lightbulb came on in my head :D

Patrick Donahue
Patrick Donahue
9,523 Points

It looks right to me. But if you are doing a challenge you have album spelled wrong.

NSDictionary *ablbum should be NSDictionary *album

thanh trung
thanh trung
4,246 Points

i know , i changed it but it still gave me an error message. it said " NSDictionary doesnt have any value". i have tried with xcode it work fine

Scott Evans
Scott Evans
4,236 Points

Dont forget to Assign best answer so that others can see that this has been solved and possibly benefit from the help