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 trialMatthew Grant
3,703 PointsUnsure about how to pass Part 2 of Dictionary task
I've been tinkering my code for about half an hour but I have hit a roadblock on how to pass part 2 of the dictionary task.
This is my code at the moment:
let currencies = [ "US": "Dollar", "UK": "Pound", "JP": "Yen"] let ukCurrency = currencies.removeValueForKey("UK")
The error message I receieve on Xcode is:
Playground execution failed: <EXPR>:13:18: error: immutable value of type '[String : String]' only has mutating members named 'removeValueForKey' let ukCurrency = currencies.removeValueForKey("UK")
If you know how to resolve please reply to post.
Thank you :).
4 Answers
Damian Adams
21,351 PointsThe problem with the code challenge in particular is that it won't permit you to change your dictionary type from a constant to a variable, obstructing you from being able to assign a removed key value to your new ''ukCurrency'' constant because the dictionary you're working on is immutable.
I hope they can fix this soon, or maybe I'm missing something.
José Manuel Martínez López
18,133 Pointslet ukCurrency = currencies["UK"]
George Blizzard
Courses Plus Student 285 PointsThis gave me the correct answer, but I don't understand why this is correct and let ukCurrency = currencies.removeValueForKey("UK") is incorrect. The example shown in the video uses that code; let ukCurrency = currencies["UK"] is never demonstrated.
José Manuel Martínez López
18,133 PointsBecause a constant value is immutable.
Michael Liendo
15,326 Pointshello :) the error message is saying that your dictionary named ukCurrency is a constant...so it can't be changed, yet your trying to remove a member using the 'removeValueForKey' method.
In other words, your dictionary is immutable, you have to make it mutable, hope that helps!
Taylor Smith
iOS Development Techdegree Graduate 14,153 Pointseverything Michael Liendo said. but for us common folk, instead of using "let currencies" use "var currencies." let makes it a constant which can't be changed. var makes it a variable which can be changed. :)