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

Michael Snyder
PLUS
Michael Snyder
Courses Plus Student 7,502 Points

Help with this please.

Object Oriented Objective-C Challenge Task 3 of 3

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

[shoeOrderDict setValue:@([shoeOrder objectAtIndex:0]) forKey:@"customer"];
[shoeOrderDict setValue:@([shoeOrder objectAtIndex:1]) forKey:@"size"];
[shoeOrderDict setValue:@([shoeOrder objectAtIndex:2]) forKey:@"style"];
[shoeOrderDict setValue:@([shoeOrder objectAtIndex:3]) forKey:@"color"];

3 Answers

Keli'i Martin
Keli'i Martin
8,227 Points

You should be using setObject:forKey, not setValue:forKey.

Hope that helps!

Michael Snyder
Michael Snyder
Courses Plus Student 7,502 Points

yep! just gave you a big thanks on another question thread!

Keli'i Martin
Keli'i Martin
8,227 Points

You know, going back, the problem isn't actually setObject: vs setValue:. Both actually will work. What you have above isn't working because you don't need to put [shoeOrder objectAtIndex:0] inside the @( ). Apologies! :)

Michael Snyder
PLUS
Michael Snyder
Courses Plus Student 7,502 Points

So you are saying I could've done :

[shoeOrderDict setValue:[shoeOrder objectAtIndex:0] forKey:@"customer"];

or...

[shoeOrderDict setValue:@[shoeOrder objectAtIndex:0] forKey:@"customer"];

??

Keli'i Martin
Keli'i Martin
8,227 Points

No, what I'm saying is setValue:forKey: and setObject:forKey: would have both worked to solve the challenge. You don't want to do @[shoeOrder objectAtIndex:0] because that is not correct syntax. The @[] is used to define an array, which is not what you're doing here. You're actually calling a method ([shoeOrder objectAtIndex:0]).

So, in a nutshell:

[shoeOrderDict setValue:[shoeOrder objectAtIndex:0] forKey:@"customer"];

and

[shoeOrderDict setObject:[shoeOrder objectAtIndex:0] forKey:@"customer"];

both would have worked.