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 Basics (retired) Collections What is a Dictionary?

riyajain23
riyajain23
2,348 Points

Retrieving Info From Dictionaries

I was supposed to create a constant, called "currencies", and then assign the following values to it: (US: Dollar, UK: Pound, JP: Yen). This worked, but then it asked me to retrieve a value. I was supposed to retrieve the value for UK currency (Pound), and assign it to the constant ukCurrency. This is where the code was apparently incorrect. The error is in the retrieving line. What's the error, and how can I solve it?

Andrew Rodko
Andrew Rodko
27,881 Points

Hi, could you show me the code you have for this so far?

riyajain23
riyajain23
2,348 Points

let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]

let ukCurrency = currencies.removeValueForKey("UK")

2 Answers

Andrew Rodko
Andrew Rodko
27,881 Points

The issue is that you're attempting to remove the value from the dictionary, and the dictionary is defined as a constant. This would work if you define currencies with var instead of let to make it mutable, but here's the best answer for just retrieving the value: let ukCurrency = currencies["UK"]

riyajain23
riyajain23
2,348 Points

Okay. Thanks so much!

Hello Vipin,

In this objective you need to create a dictionary using the specified KV (key-value pairs).

let currencies = ["US" : "Dollar", "UK" : "Pound", "JP" : "Yen"]

Once the dictionary is ready you are asked to retrieve "UK" currency. You can retrieve a value by simply calling it's key:

let currencies = ["US" : "Dollar", "UK" : "Pound", "JP" : "Yen"]

let ukCurrency = currencies["UK"]