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
Brendan Freeman
Courses Plus Student 297 PointsWhat is the part that I am missing for the ukCurrency constant coding
I was just want to write the code
Brendan Freeman
Courses Plus Student 297 Pointslet currencies = [ "US": "Dollar", "UK": "Pound", "JP": "Yen" ] let ukCurrency = currencies.removeValueForKey ("UK")
4 Answers
Stone Preston
42,016 Pointstask 2 states: Assign the value for key "UK" to a constant named ukCurrency.
you can access values for keys in dictionaries by using subscript syntax which means placing the key in between brackets after the name of the dictionary:
let someValue = someDict["someKey"] // returns the value for the key of "someKey" in someDict
you need to access the value in the currencies dict with a key of "UK".
let currencies = [ "US": "Dollar", "UK": "Pound", "JP": "Yen"]
let ukCurrency = currencies["UK"] // ukCurrency now has a value of "Pound"
see the Swift eBook for more information on accessing and modifying dictionaries
Brendan Freeman
Courses Plus Student 297 Pointsok thank you but why does adding the comment at the end allow it to work im just want to know for future reference
Stone Preston
42,016 Pointscomments are ignored by the compiler. they are not treated as "code" they are just there for the reader/writer of the code to reference.
Brendan Freeman
Courses Plus Student 297 Pointsso the fact that u recalled the constant on the second part is what made the difference then right
Stone Preston
42,016 Pointsyes
let ukCurrency = currencies["UK"]
basically says "give me the value from the currencies dictionary that has a key of "UK" and it returns "Pound" and that gets assigned to the constant ukCurrency
Brendan Freeman
Courses Plus Student 297 Pointsok I follow you thank you Im still trying to get the hang of everything
Stone Preston
42,016 Pointsno problem
Stone Preston
42,016 PointsStone Preston
42,016 Pointscan you post what you have tried so far