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 trialriyajain23
2,348 PointsRetrieving 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?
riyajain23
2,348 Pointslet currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]
let ukCurrency = currencies.removeValueForKey("UK")
2 Answers
Andrew Rodko
27,881 PointsThe 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
2,348 PointsOkay. Thanks so much!
swiftfun
4,039 PointsHello 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"]
Andrew Rodko
27,881 PointsAndrew Rodko
27,881 PointsHi, could you show me the code you have for this so far?