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?

Need help with dictionaries

In the quiz it asks to assign a constant to the library. Im confused because I have no step by step... the video bounces around. Could i get a step by step example? I am thinking I would make the dictionary first then somehow call the dictionary currencies and make it constant. Please explain

dictionaries.swift

2 Answers

Stone Preston
Stone Preston
42,016 Points

task 1 states: 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.

we need to create a constant and assign it a dictionary.

you create a constant using let and assign it a value using = followed by the value you want to assign it.

dictionaries look like sets of key value pairs seperated by colons. Multiple key value pairs are separated with commas. a dictionary is enclosed with brackets []

so to create a constant named currencies and assign it the dictionary you would use:

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

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

we need to retrieve a value from the dictionary using a specific key and assign that value to a constant. you can retrieve values from a dictionary using dictionaryName["keyName"]

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

let ukCurrency = currencies["UK"]

GREAT! THANKS FOR THE HELP!