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 trialAmir Bahadori
5,498 PointsDictionaries and Arrays Challenge Task 3 of 3: Help needed on task 3
Now add the values in our array to our new dictionary. The keys for the values should be: 'customer', 'size', 'style' and 'color'. The values in the original array are in the same order as the keys listed above. (Note: Don't set the values directly as strings, but rather use something like 'objectAtIndex' to reference the original array.)
NSArray *shoeOrder = @[ @"Charles Smith", @(9.5), @"loafer", @"brown"]; NSMutableDictionary *shoeOrderDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: nil];
NSArray *shoeOrder = @[ @"Charles Smith", @(9.5), @"loafer", @"brown"];
NSMutableDictionary *shoeOrderDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: nil];
1 Answer
Steve Hunter
57,712 PointsHi Amir,
You've got the array set up, so you now need to populate the dictionary with the contents of that array, and the keys given in the question.
You can do this one step at a time, using each key in turn using literal syntax. So, once you've you've declared the dictionary, with alloc and init, you can use that variable name and add each new key and assign the value from the array, one element at a time.
That looks like:
NSArray *shoeOrder = @[@"Charles Smith", @(9.5), @"loafer", @"brown"];
NSMutableDictionary *shoeOrderDict= [[NSMutableDictionary alloc] init];
shoeOrderDict[@"customer"] = [shoeOrder objectAtIndex: 0];
shoeOrderDict[@"size"] = [shoeOrder objectAtIndex: 1];
shoeOrderDict[@"style"] = [shoeOrder objectAtIndex: 2];
shoeOrderDict[@"color"] = [shoeOrder objectAtIndex: 3];
I hope that helps,
Steve.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI guess you could also use something like:
[shoeOrderDict setValue:[shoeOrder objectAtIndex:0] forKey:@"customer"];
I'm not sure that adds any clarity or advantage - but it looks OK to me.
Amir Bahadori
5,498 PointsAmir Bahadori
5,498 PointsThanks Steve, I looked around to find the syntax to add an object from an existing array into a mutable dictionary and couldn't find anything. Appreciate it.