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?

Brad Hopkins
Brad Hopkins
4,125 Points

Assign a value for a key that is in a constant?

Task 1 of the Dictionary challenge asked us to create a dictionary that is a constant that includes currencies (UK: Pound, US: Dollar, JP: Yen) which I created and passed.

In the next step, it asks to Assign the value of ukCurrency to the constant UK.

I can't get past this part. Is it possible to reassign a value for a constant? I can do it if the dictionary is a variable (as in all of the examples) but I can't pass this task because it needs to be in a constant.

I have tried this in Xcode and can't get it to work in a constant without throwing errors as well.

This works (as a variable):

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

But doesn't as a constant.

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

Xcode is giving an error: '@lvalue $T5' is not identical to '(String, String)'

I'm sure I'm missing something, I just can't figure out what. I've watched the video numerous times and checked the Swift documentation on Dictionaries (https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-XID_180) but can't find a workable solution.

Any direction would be greatly appreciated.

4 Answers

Chris Shaw
Chris Shaw
26,650 Points

Hi Brad,

Just to put it into context this is the question.

Assign the value for key "UK" to a constant named ukCurrency.

What it's asking is for you to create a constant called ukCurrency and assign the value for the key of UK to the ukCurrency constant.

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

Is it possible to reassign a value for a constant?

No it isn't, constants are designed as read-only so once the value is assigned you would need to clone it first before you can alter the values but the purpose of them is that you don't need to change the values once their set, however if you have a constant that has properties and methods you can alter those values but again you can reassign the value of the constant.

Hey Brad,

I think you might be looking into it too much. I believe the question is simply asking to define a new constant with the value inside of it like:

let newVariable = arrayName["key"]

So for your example:

// 1. Create the array
let currencies = ["US": "Dollar","UK": "Pound", "JP":"Yen"]

// 2.  Set the Key for UK to a new constant
let ukCurrency = currencies["UK"]

Hope this helps!

Brad Hopkins
Brad Hopkins
4,125 Points

Thanks everyone. Yes, as you figured out, I misinterpreted the task at hand. The directions that you gave helped me solve the challenge! Moving on to Loops!

Thanks, it worked.