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! Review Dictionaries and Arrays

Sara Worth
Sara Worth
5,225 Points

Still need help on this question

I can't figure out how to add elements from the array to the dictionary. I've re-watched the videos and looked for the answer online but I'm still doing something wrong.

variable_assignment.mm
NSArray *shoeOrder = @[@"Charles Smith", @(9.5), @"loafer", @"brown"];
NSMutableDictionary *shoeOrderDict;
shoeOrderDict = [[NSMutableDictionary alloc] init];

shoeOrderDict[@customer] = @[shoeOrder objectAtIndex: 0];
shoeOrderDict[@size] = @[shoeOrder objectAtIndex: 1];
shoeOrderDict[@style] = @[shoeOrder objectAtIndex: 2];
shoeOrderDict[@color] = @[shoeOrder objectAtIndex: 3];

1 Answer

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,858 Points

Hey Sara,

Your code is 100% correct... the problem lies with the quotation marks around the four values (the last 4 lines of code). If you notice in the code above, they are highlighted, and in the Challenge Editor, I'm betting the strings weren't color formatted like the strings in the first line of code. This is because these quotation marks are "fancy quotes" and are not recognized by the code engine as being actual quotes (I'm guessing you copy/pasted from another editor into the challenge?).

So, all you need to do is go and change those quotes to regular quotes (just delete and type in from the challenge), and all three tasks will pass. Keep this in mind when doing a copy/paste (which there is nothing wrong with). The quotes may not be 'real' quotation marks.

Edited: Please see comment below for an additional syntax error not seen when this was first answered.

Also, just a tip to keep things DRY. You could do these 2 lines:

NSMutableDictionary *shoeOrderDict;
shoeOrderDict = [[NSMutableDictionary alloc] init];

as one line:

NSMutableDictionary *shoeOrderDict = [[NSMutableDictionary alloc]init];

Keep Coding! :) :dizzy:

Sara Worth
Sara Worth
5,225 Points

Hmm... I tried re-typing everything and it still doesn't pass.

Jason Anders
Jason Anders
Treehouse Moderator 145,858 Points

Sorry Sara Worth

I kind of focused on the quotes and missed that you have the @ symbol before the array objectAtIndexlines which is not correct. These don't need the @ symbol as they are retrieving information rather than creating the object. So

shoeOrderDict[@"customer"] = @[shoeOrder objectAtIndex: 0];
shoeOrderDict[@"size"] = @[shoeOrder objectAtIndex: 1];
shoeOrderDict[@"style"] = @[shoeOrder objectAtIndex: 2];
shoeOrderDict[@"color"] = @[shoeOrder objectAtIndex: 3];

should be

shoeOrderDict[@"customer"] = [shoeOrder objectAtIndex: 0];
shoeOrderDict[@"size"] = [shoeOrder objectAtIndex: 1];
shoeOrderDict[@"style"] = [shoeOrder objectAtIndex: 2];
shoeOrderDict[@"color"] = [shoeOrder objectAtIndex: 3];

My apologies for the confusion. :dizzy:

Sara Worth
Sara Worth
5,225 Points

Thank you - that worked!