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 trialBrad Hopkins
4,125 PointsAssign a value for a key that is in a constant?
Task 1 of the Dictionary challenge asked us to create a dictionary that is a constant that includes currencies (UK: Pound, US: Dollar, JP: Yen) which I created and passed.
In the next step, it asks to Assign the value of ukCurrency to the constant UK.
I can't get past this part. Is it possible to reassign a value for a constant? I can do it if the dictionary is a variable (as in all of the examples) but I can't pass this task because it needs to be in a constant.
I have tried this in Xcode and can't get it to work in a constant without throwing errors as well.
This works (as a variable):
var currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]
currencies["UK"] = "ukCurrency"
But doesn't as a constant.
let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]
currencies["UK"] = "ukCurrency"
Xcode is giving an error: '@lvalue $T5' is not identical to '(String, String)'
I'm sure I'm missing something, I just can't figure out what. I've watched the video numerous times and checked the Swift documentation on Dictionaries (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-XID_180) but can't find a workable solution.
Any direction would be greatly appreciated.
4 Answers
Chris Shaw
26,676 PointsHi Brad,
Just to put it into context this is the question.
Assign the value for key "UK" to a constant named ukCurrency.
What it's asking is for you to create a constant called ukCurrency
and assign the value for the key of UK to the ukCurrency
constant.
let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]
let ukCurrency = currencies["UK"]
Is it possible to reassign a value for a constant?
No it isn't, constants are designed as read-only so once the value is assigned you would need to clone it first before you can alter the values but the purpose of them is that you don't need to change the values once their set, however if you have a constant that has properties and methods you can alter those values but again you can reassign the value of the constant.
Cody McClintock
9,391 PointsHey Brad,
I think you might be looking into it too much. I believe the question is simply asking to define a new constant with the value inside of it like:
let newVariable = arrayName["key"]
So for your example:
// 1. Create the array
let currencies = ["US": "Dollar","UK": "Pound", "JP":"Yen"]
// 2. Set the Key for UK to a new constant
let ukCurrency = currencies["UK"]
Hope this helps!
Brad Hopkins
4,125 PointsThanks everyone. Yes, as you figured out, I misinterpreted the task at hand. The directions that you gave helped me solve the challenge! Moving on to Loops!
GURUPRASAD NAGARAJAN
7,853 PointsThanks, it worked.