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 Swift Basics (retired) Collections What is a Dictionary?

Jacob Tucker
Jacob Tucker
512 Points

why doesnt the following statement work let ukCurrency = currencies.removeValueForKey ("UK")

assuming first line is correct which it is

dictionaries.swift

1 Answer

Stone Preston
Stone Preston
42,016 Points

you misinterpreted what the task is asking you to do.

task 2 states: Assign the value for key "UK" to a constant named ukCurrency.

you dont remove the value assigned to the key "UK", you assign the value of that key to a new constant, whilst keeping the dictionary the same (you dont want to remove any keys/values or anything like that)

the swift eBook states:

You access and modify a dictionary through its methods and properties, or by using subscript syntax

subscript syntax consists of the name of the dictionary followed by a pair of square brackets with the key inside:

someDictionary["someKey"]

the above code returns the value for the key "someKey" in the dictionary named someDictionary

so to assign the value for the key "UK" to a constant named ukCurrency you would use

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

let ukCurrency = currencies["UK"] // ukCurrency now has a value of "Pound"