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 trialAnthony Price
1,412 PointsiOS Development Stage 4 Question
iOS Development with Swift Stage 4: Collections
Challenge Task1:
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.
Answer:
let currencies = ["US":"Dollar", "UK":"Pound", "JP":"Yen"]
This answer is seen as correct by program
Challenge Task 2:
Assign the value for key "UK" to a constant named ukCurrency.
Answer:
let currencies = ["US":"Dollar", "UK":"Pound", "JP":"Yen"]
In the example shown by Amit he used the declaration ‘removeValueForKey’…which I assume would look like the below:
let ukCurrency = currencies.removeValueForKey("UK")
And this would work if the constant ‘currencies’ was a variable but the program results in error asking “currencies needs to be a constant’.
Can anyone help me here? I’m at a loss.
Thanks, Tony
2 Answers
Stone Preston
42,016 Pointstask 2 states: Assign the value for key "UK" to a constant named ukCurrency.
you just need to assign the value the key "UK" is paired with to a constant. you dont have to modify the dictionary at all, just retrieve a value from it.
you can access the value stored in a dictionary by using the name of the dictionary and the corresponding key in brackets like so: dictionaryName["keyName"]
let currencies = ["US":"Dollar", "UK":"Pound", "JP":"Yen"]
let ukCurrency = currencies["UK"]
Anthony Price
1,412 PointsThanks Stone!
Alex Grant
812 PointsAlex Grant
812 PointsI just posted about this as I had similar confusion. Seems like you can read but obviously not manipulate a constant.