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?

ROB TILLEY
ROB TILLEY
842 Points

Assign the value for key "UK" to a constant named ukCurrency.

Tried the answers on the discussion forum but does not work. The question is not asking for anything that was covered in the training. Need help pls.

dictionaries.swift
let currencies = ["US":"Dollar","UK":"Pound","JP":"Yen"]
let ukCurrencies = currencies["UK"]

2 Answers

You have it right with the exception of one typo: You have assign the value for the key "UK" to 'ukCurrencies', it needs to be ukCurrency.

let currencies = ["US":"Dollar","UK":"Pound","JP":"Yen"]
let ukCurrency = currencies["UK"]
ROB TILLEY
ROB TILLEY
842 Points

Thanks Kyle. As an attempt to understand what is going on here, does this code look into the constant named "currencies" find the value for the code "UK" and then use it as a value for a newly created constant named "ukCurrency"?. If yes, what would be a real world example of why you would want to do this?

Jon Schenck
Jon Schenck
2,028 Points

Your second constant needs to be changed. It should look like this...

let ukCurrency = Pound

Jon Schenck
Jon Schenck
2,028 Points

If this is helpful I would appreciate it if you click best answer. Thank you.

Jon, although this will pass the challenge, it isn't exactly what the question is asking you to do. This is all about capturing values, so even though "Pound" is the value for the key "UK" and therefore technically a satisfying answer to the compiler here, assigning the value for the key in a dictionary to a new constant is not the same as simply assigning that key's value String as itself to a new constant. It needs to be like this

let someConstant = someDictionary["someKey"]
Jon Schenck
Jon Schenck
2,028 Points

So basically whatever makes the value equal to pound is what will make the compiler accept the answer. I see what you have done. Thank you for further explaining this. I appreciate it.