Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Swift Basics (retired) Collections What is a Dictionary?

Ryan Anderson
Ryan Anderson
4,610 Points

Dont understand challenge task 2 of 2

show me how to do it

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

let ukCurrency = currencies

3 Answers

Ryan Jin
Ryan Jin
15,337 Points

Like an array, but instead you type in the key, not the index

let ukCurrency = currencies["UK"]
Matthew Wiese
Matthew Wiese
1,659 Points

The answer it is looking for is...

let ukCurrency = currencies["UK"]

But I think the question can also be answered (based on the video) like this...

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

let ukCurrency = currencies.removeValueForKey("UK")

If you use this one, the Teamtreehouse compiler will throw an error. It will not throw an error in xCode however.

Hope this helps!

Your second answer actually will not work because the dictionary currencies is declared as a constant, meaning that the the key-value pairs are immutable. You may capture a value in the dictionary, but you cannot remove or change the key-value pairs as long as the dictionary is declared as a constant. Besides, the challenge clearly asks you to CAPTURE one of the dictionary's values and assign it do a new constant, not MUTATE the original dictionary.

i resolved doing this let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"] let ukCurrency = currencies["UK"]