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 Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! Alloc and Init

Hi guys. For some reason your site isn't allowing me to pass this working code.

NSMutableDictionary *carDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"Make" , @"Honda", @"Model" , @"Accord", nil];

But your site says it is wrong and to check my syntax :)

Any ideas??

Thanks in advance D

variable_assignment.mm
NSMutableDictionary *carDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"Make" , @"Honda", @"Model" , @"Accord", nil];

1 Answer

Stuart Wright
Stuart Wright
41,120 Points

Although the way you've done it would seem more intuitive (to me, at least), you actually have the keys and values the wrong way around. The value comes before the key for each pair, according to the documentation:

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: @"value1", @"key1", @"value2", @"key2", nil];

https://developer.apple.com/documentation/foundation/nsdictionary/1574190-initwithobjectsandkeys

Thanks so much!!! I knew it had to be something simple like that as my coding was fine. That little line drove me mad for hours :) Finally passed it!!

The answer was: NSMutableDictionary *carDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"Honda" , @"Make", @"Accord" , @"Model", nil]; NSString *myRide = [[NSString alloc] init]; myRide = [carDict valueForKey:@"Make"];