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?

Rod Gray
Rod Gray
592 Points

What is a dictionary? task challenge error...

Hi,

Getting error message with this challenge...

First part of the challenge is to set up a dictionary and assign them to a constant called currencies.

"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."

No problem there.

However in the 2nd part of the challenge it's asking to 'Assign the value for key "UK" to a constant named ukCurrency'

The error message I'm getting is :

swift_lint.swift:4:18: error: immutable value of type '[String : String]' only has mutating members named 'removeValueForKey' let ukCurrency = currencies.removeValueForKey("UK")

I could be wrong (most likely) but the error message is suggesting that because the dictionary is assigned to a constant, it won't let you remove an item from that dictionary?

I've redone this with the dictionary set up with var instead of let and works fine.

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

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Rod,

This challenge has caught a number of users off-guard, not because of a lack of information but because of how the question is worded in regard to the last example in the previous video. What the task means by Assign the value for key "UK" is, you need to use subscript to select the value via the key name within square brackets, which is shown at the start of the previous video.

So instead of removing the value from the dictionary which you've attempted to do above, all you need is the below.

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

Happy coding!

Rod Gray
Rod Gray
592 Points

Thanks Chris. Went through the video again and found what you were referring to. All clear now. :)

Greg Kaleka
Greg Kaleka
39,021 Points

Hi Rod,

Your thinking is exactly right - you cannot call removeValueForKey on an immutable dictionary. Since you've used let, currencies is a constant, and therefore immutable.

We don't need to use removeValueForKey, though! In fact, you don't need to call any methods on currencies. Instead, we'll simply use sub-scripting to index into the dictionary using the key, and use the value:

solution.Swift
let currencies = ["US": "Dollar", "UK": "Pound", "JP": "Yen"]
let ukCurrency = currencies["UK"]
Rod Gray
Rod Gray
592 Points

Thanks Greg. Glad to hear I was on the right path as far as the error message was concerned. I've gone back through the video again and found where I was going wrong with the code. :)