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 trialThomas Thomas
1,706 PointsHow do I assign a constant as part of a new key pair?
In the lesson it discussed dictionaries as variables, but in the challenge its asking for the dictionary to be made as a constant. I can't seem to figure out how to answer the question and I'm wondering if it's because the constant dictionary can't be changed?
9 Answers
Vittorio Somaschini
33,371 PointsExactly, you are not supposed to remove anything here Thomas.
Just declare a constant named ukCurrency, but without modifying the dictionary.
This will work:
let ukCurrency = currencies["UK"]
So that we retrieve the value associated to the "UK" key, but the dictionary stays as it is.
Hope it helped
Vittorio Somaschini
33,371 PointsHello Thomas.
I am not sure I understand your question properly but..
The challenge is simply asking you to create a dictionary and assign it to a constant (let), so:
let currencies =[]
Then, inside the [] you should put the keys with their relative values, for example:
"UK": "Pound"
That is all, I did not quiet understand the "assign a constant as part of a new key" part..
Vittorio Somaschini
33,371 PointsCan I see your code please?
Paste it here, because that code should not edit the dictionary anyhow (so the dictionary can be a constant with no problems), the code should only retrieve a value without "touching" the dictionary.
;)
Vittorio Somaschini
33,371 PointsAre you sure?
I have just copied and pasted your code + my code and it worked fine:
let currencies = [ "US":"Dollar", "UK":"Pound", "JP":"Yen"]
let ukCurrency = currencies["UK"]
Can you try copy/paste this?
Vittorio Somaschini
33,371 PointsI was going to guess something like that.
You should give me a best answer mark at least then!
ahah
;)
good luck
Thomas Thomas
1,706 PointsVittorio,
I am on the second part of the challenge where it's asking to:
"Assign the value for key "UK" to a constant named ukCurrency."
That's where it's stumping me. I've tried to do it on Xcode to see if I could resolve but I keep getting an error message of some sort telling me that I'm attempting to change an immutable value type. (because i made it a constant dictionary).
I'm sure I'm just missing something simple.
Thomas Thomas
1,706 PointsVittorio,
I am on the second part of the challenge where it's asking to:
"Assign the value for key "UK" to a constant named ukCurrency."
That's where it's stumping me. I've tried to do it on Xcode to see if I could resolve but I keep getting an error message of some sort telling me that I'm attempting to change an immutable value type. (because i made it a constant dictionary).
I'm sure I'm just missing something simple.
Thomas Thomas
1,706 Pointslet currencies = [ "US":"Dollar", "UK":"Pound", "JP":"Yen"]
let ukCurrency = currencies.removeValueForKey("UK")
Thomas Thomas
1,706 PointsSorry Vittorio, I was using () instead of []
Thank you so much for your help!!! You fixed about an hour of frustration!!