Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Objective-C Basics (Retired) Foundation Framework NSDictionary

Add to your mutableArray with a key of year and value of 1969

I'm trying to add the year 1969 onto my mutable dictionary, but it keeps saying I haven't set it to the right value. I swear I have this set up the same as the example project!

nsdictionary.mm
NSDictionary *album = @{@"title":@"Abbey Road", @"artist":@"The Beatles"};
NSMutableDictionary *albumMutable = [NSMutableDictionary dictionaryWithDictionary:album];
[albumMutable setObject:@1969 forKey:@"year"]; 

1 Answer

ObjectiveC doesn't like int types (literal numbers) to be used as keys as it wants some form of object. You could either create an NSNumber object out of 1969, or you could also just set the value as a NSString of "1969" which looks like what you were starting to do. In the latter case all you need to do is add quotes around it:

@"1969"

That's odd because xcode is pretty happy with this:

    NSMutableDictionary *mutableBook = [NSMutableDictionary dictionaryWithDictionary:book];
    [mutableBook setObject:@1957 forKey:@"year"];
    NSLog(@"%@", mutableBook);

But yes! That has worked, thanks :-)