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?

Error on Task 2 (Dictionaries)

Why do I get this following message in the preview section? "swift_lint.swift:4:18: error: immutable value of type '[String : String]' only has mutating members named 'removeValueForKey' let ukCurrency=currencies.removeValueForKey("UK")"

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

2 Answers

The task is not asking you to remove a value for a certain key in the dictionary. You are getting this error because the dictionary is declared as a constant using the let keyword, therefore cannot have one of it's key's value removed. All the second task is asking of you is to assign the value for the key "UK", which is "Pound", to another constant. Like this:

let currencies = [ "US": "Dollar", "UK": "Pound", "JP": "Yen" ]
let ukCurrency = "Pound"
Allan Clark
Allan Clark
10,810 Points

It is giving you that error because you are trying to remove a value from a dictionary saved as a constant. That is what the removeValueForKey() method does. In order to just access the key value pair you do something similar to an array. The last line should look like this.

let ukCurrency = currencies["UK"]

Thank You Allan