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 Swift Collections and Control Flow Dictionaries in Swift Working With Dictionaries

jonathan Appiah Bannor
jonathan Appiah Bannor
444 Points

help for the task 2

can anyone give a help with the task 2? please many thanks Jonathan

dictionary.swift
// IceCream 

var iceCream = ["CC": "Chocolate Chip",
                 "AP": "Apple Pie",
                 "PB": "Peanut Butter"]
iceCream.updateValue : ["Rocky Road", "RR"]

2 Answers

Eric Haslag
Eric Haslag
9,507 Points

Hi Jonathan,

It might help to check out the documentation for the Dictionary.updateValue() method. Here's a link to it: updateValue(_:forKey:).

It looks like you're really close. All you need to do is put your values into the method parameters. For example, if I wanted to add "Cookie Dough" for the key "CD" I'd call the method like this:

iceCream.updateValue("Cookie Dough", forKey: "CD")

There's also a shorthand way to do this by using subscript syntax:

iceCream["CD"] = "Cookie Dough"
Matthew Long
Matthew Long
28,407 Points

It kind of looks like you're trying to combine the two ways to solve this part of the challenge.

First, updateValue is a method. The syntax for using the method was given in the problem description: updateValue(Value, forKey: Key). This looks like:

var iceCream = ["CC": "Chocolate Chip",
                 "AP": "Apple Pie",
                 "PB": "Peanut Butter"]

iceCream.updateValue("Rocky Road", forKey: "RR")

You could also solve this by using simple subscript syntax:

var iceCream = ["CC": "Chocolate Chip",
                 "AP": "Apple Pie",
                 "PB": "Peanut Butter"]

iceCream["RR"] = "Rocky Road"

Both ways accomplish the task at hand! Happy coding! :smile: