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?

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

dictionaries.swift
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