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 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"]