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

Luis Paulino
PLUS
Luis Paulino
Courses Plus Student 1,779 Points

I can't figure out my syntax error, and every time I write code my help button won't work. sorry :(.

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

2 Answers

Addison Francisco
Addison Francisco
9,561 Points

There are a few things going on with your code that should be changed. First, the challenge asks you to write it all in one line of code. Second, the message you are passing to NSMutableDictionary has a typo in it

// Change this
initWithObjectsAndKey:

// To this
initWithObjectsAndKeys:

Third, the structure of your values and keys are mixed up and you are also using colons where you shouldn't be for this init method. Here is an example from the Apple documentation on how to write this kind of initializer

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

If you look at the order of your values and keys compared to this example, you should see that yours are backwards

// Your dictionary
@"Make":@"Honda", @"Model":@"Accord"

// Documentation example dictionary
@"value1", @"key1", @"value2", @"key2"

Now, if we put this all together, you should have something like this

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

Hope this was helpful for you. Be sure to always refer to the Apple Documentation :)