Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,381 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: