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

NSMutableDictionary. Adding a value and key problems...

Im a little bit confused why the instructions say to add a NSString when I thought I should be adding [mutableAlbum setObject:@1969 forKey:@"year"];

So here is what I have:

NSDictionary *album = @{ @"title":@"Abbey Road", @"artist":@"The Beatles" }; NSMutableDictionary *albumMutable = [NSMutableDictionary dictionaryWithDictionary:album]; NSString *year =[albumMutable objectForKey:@"1969”]; or [albumMutable setObject:@1969 forKey:@"year”];

I know both of the last lines are incorrect but would like someone to explain to me why. Thanks in advance!

--Jeff

1 Answer

Ben Falk
Ben Falk
3,167 Points

This is the question: Let's add the year of the album to the 'albumMutable' variable. Where the key is a NSString 'year' and the value is also a NSString '1969'.

I don't really think there is a specific reason why an NSString is asked for, other than perhaps to make sure you are actually understanding the information, and not blindly copying from the video. I agree that an NSNumber, like "@1969" probably makes more sense.

NSString *year =[albumMutable objectForKey:@"1969”]; This doesn't work because it isn't what the question is asking for. Instead of adding an item to the albumMutable variable, this line creates a new NSString, which tries to access the album information and retrieve the value for key "1969". You're retrieving info rather than setting it.

[albumMutable setObject:@1969 forKey:@"year”]; This doesn't work because while you are setting an object, the question is expecting the value for "year" to be a string, whereas the code here sets it to an NSNumber. Since the question asks for the value to be a string, you'll need to surround the year with quotation marks in order for it to work.