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

Is this Key value question a bug? Will not let me move forward.

Question: Let's create a dictionary that contains the currencies of the various countries along with their country codes. The country code will be the key and the currency the value. Using the following key-value pairs: US : Dollar, UK : Pound, JP : Yen assign them to a constant named currencies.

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

SAYS IT'S CORRECT AND MOVES FORWARD TO THIS: Question: Assign the value for key "UK" to a constant named ukCurrency.

MY ANSWER: let ukCurrency = currencies.removeValueForKey("UK")

Keeps giving me an error... All types of errors... depends on how I keep changing the code around.

In XCode I use this and I get no Errors (Notice the var instead of let): var currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"] let ukCurrency = currencies.removeValueForKey("UK")

So what is the deal here? I think that this constant erroring and forcing me to try different things is counter-productive to me actually learning. Is this an error in this program/quiz?

Thank you

3 Answers

Hey Kirk,

the answer is actually pretty simple:

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

You assign the value of "currencies" to a new constant. What you did is remove a value from the dictionary. In real code this is also valid, but the question was to assign a given value to a new constant.

Hope I could help you with that. :-)

Hey Kirk,

Is the second question basically stating to now modify the existing code to use a constant for 'UK'. Because currencies is defined as a constant, you can't modify it, so your code should be something like:

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

Sorry if this is off, I haven't started the Swift course yet. :)

Yes that was helpful. I can't believe I didn't try that! Actually, I tried currencies ("UK")

Maybe I was too sleepy! but anyway, thank you mucho!

You're very welcome.

Actually currencies() is initializer syntax, that means that you would initialize a new instance of currencies (if currencies was a class ;-)). Remember, if you want to retrieve a value from an array or dictionary always use [] after the name of the array or dict.