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?

Harrison Merkt
Harrison Merkt
1,659 Points

how is this wrong?

It's saying that I need to assign ukCurrency the value of "UK" from the currencies dictionary. This syntax works in Xcode and I don't see how it's wrong.

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

let ukCurrency = "UK"
Taylor Amsler
Taylor Amsler
4,427 Points

Hi Harrison. In your current code, you aren't assigning ukCurrency a value from the array. Rather you are just assigning it a value (that just happens to be a value in the array). The two constants: currencies and ukCurrency are unrelated to each other.

The idea behind the challenge is to "pull out" the dictionary value using it's key "UK". So when you enter "UK" you should get out "Pound". So while the syntax is fine, it is returning "UK" because, once again, the two constants aren't related.

To pull values out of dictionaries, try using the array name and the square brackets:

let ukCurrency = currencies["UK"]

Hope this helps!

1 Answer

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

The challenge isn't asking you to assign ukCurrency the value of "UK". It's asking you to assign ukCurrency the value inside the dictionary currencies that corresponds to the key "UK". To do this, you must use subscripting syntax:

let ukCurrency = currencies[/*INSERT KEY HERE*/]

I hope this helps!