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?

Robert Harmon
Robert Harmon
1,464 Points

Stuck on "Dictionaries" challenge task 2 of 2. Have I made an error?

The challenge is to "Create a constant named ukCurrency. Assign the value from the currencies dictionary. Use the key of "UK" to access the value."

This is what I have so far :

let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"] let ukCurrency = currencies.removeValueForKey("UK")

What am I missing? Thanks in advance!!

dictionaries.swift

3 Answers

Hi Robert,

Your code changed the dictionary. You removed a key/value pair.

The tests for this challenge will test that the value of your ukCurrency variable is as expected and that the dictionary itself remains unaltered. Your code created an outcome different to the one expected.

Make sense?

Steve...

Robert Harmon
Robert Harmon
1,464 Points

Ah, got it! So even though I assigned ukCurrency the correct value by removing "Pound" from the dictionary, the test expected the dictionary to remain unaltered (to continue to contain the value "Pound"), and thus my code became incorrect. Right?

Thanks, Steve!

Spot on, yes. :-)

Robert Harmon
Robert Harmon
1,464 Points

Someone else asked the same question. The correct second line of code is:

let ukCurrency = currencies["UK"]

Robert Harmon
Robert Harmon
1,464 Points

Although, I'm still a bit confused as to why the code I tried the first time doesn't work. Can anyone explain?