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 trialAlexander Batalov
21,887 PointsIt seems to be an error in Swift basics / what is dictionary? code challenge
If I understand correctly you can not change the value in a dictionary when it defined as a constant. The challenge requires me to create a constant like that:
let currencies = ["US" : "Dollar", "UK" : "Pound", "JP" : "Yen"]
and then assign new value for UK key, so here is what I did:
let currencies = ["US" : "Dollar", "UK" : "Pound", "JP" : "Yen"]
let ukCurrency = "Bitcoin"
currencies["UK"] = ukCurrency
I've got an error: ( Bummer! Your code could not be compiled. Please click on "Preview" to view the compiler errors. )
swift_lint.swift:5:1: error: '@lvalue $T5' is not identical to '(String, String)'
currencies["UK"] = ukCurrency
^
When I played with it in playground I figured out that currencies should be a variable. And It runs perfect.
var currencies = ["US" : "Dollar", "UK" : "Pound", "JP" : "Yen"]
let ukCurrency = "Bitcoin"
currencies["UK"] = ukCurrency
currencies // => ["UK": "Bitcoin", "JP": "Yen", "US": "Dollar"]
4 Answers
Robert Bojor
Courses Plus Student 29,439 PointsHi Alexander,
It looks like you did a different version of the challenge since the current challenge asks you to create the currencies constant, and then in step 2 assign the value of the UK key to another constant called "ukCurrency". Other than that there's no more points...
let currencies = ["US":"Dollar", "UK":"Pound", "JP":"Yen"]
let ukCurrency = currencies["UK"]
Maybe they've changed it in the meantime...
Stone Preston
42,016 Pointsrobert is correct. task 2 states: Assign the value for key "UK" to a constant named ukCurrency.. it does not ask you to modify the dictionary. It asks you to use a value from the dictionary and assign that to a variable. its fine for the dictionary to be a constant, it does not need to be a variable
Alexander Batalov
21,887 PointsOh... :) Sorry. I misunderstood the question. Thank you guys!
Myers Carpenter
6,421 PointsThe reason it won't compile is that you are trying modify a constant. If you had wrote let currencies
as var currencies
you could compile the code.
But like Stone said, that's not what the challenge asked you do do.