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?

Why isn't my code compiling in Swift Basics, when it is compiling in Xcode?

Can anyone tell me why this code is not compiling in the Swift Basics tutorial? It is compiling in Xcode. The task is: Assign the value of the key UK to a constant named ukCurrency.

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

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

2 Answers

You are close. What you have done is assign ukCurrency to another array which contains only one value, "Pound". Instead, you need to assign it to just the string "Pound", because that is what the value is for the key UK.

You have two options. The preferred way is this:

let ukCurrency = currencies["UK"]

Or, you can simply write:

let ukCurrency = "Pound"

Thank you so much, Kyle! Your explanation makes perfect sense.