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?

Thomas Thomas
Thomas Thomas
1,706 Points

How do I assign a constant as part of a new key pair?

In the lesson it discussed dictionaries as variables, but in the challenge its asking for the dictionary to be made as a constant. I can't seem to figure out how to answer the question and I'm wondering if it's because the constant dictionary can't be changed?

9 Answers

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Exactly, you are not supposed to remove anything here Thomas.

Just declare a constant named ukCurrency, but without modifying the dictionary.

This will work:

let ukCurrency = currencies["UK"]

So that we retrieve the value associated to the "UK" key, but the dictionary stays as it is.

Hope it helped

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Thomas.

I am not sure I understand your question properly but..

The challenge is simply asking you to create a dictionary and assign it to a constant (let), so:

let currencies =[]

Then, inside the [] you should put the keys with their relative values, for example:

"UK": "Pound"

That is all, I did not quiet understand the "assign a constant as part of a new key" part..

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Can I see your code please?

Paste it here, because that code should not edit the dictionary anyhow (so the dictionary can be a constant with no problems), the code should only retrieve a value without "touching" the dictionary.

;)

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Are you sure?

I have just copied and pasted your code + my code and it worked fine:

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

let ukCurrency = currencies["UK"]

Can you try copy/paste this?

Vittorio Somaschini
Vittorio Somaschini
33,371 Points

I was going to guess something like that.

You should give me a best answer mark at least then!

ahah

;)

good luck

Thomas Thomas
Thomas Thomas
1,706 Points

Vittorio,

I am on the second part of the challenge where it's asking to:

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

That's where it's stumping me. I've tried to do it on Xcode to see if I could resolve but I keep getting an error message of some sort telling me that I'm attempting to change an immutable value type. (because i made it a constant dictionary).

I'm sure I'm just missing something simple.

Thomas Thomas
Thomas Thomas
1,706 Points

Vittorio,

I am on the second part of the challenge where it's asking to:

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

That's where it's stumping me. I've tried to do it on Xcode to see if I could resolve but I keep getting an error message of some sort telling me that I'm attempting to change an immutable value type. (because i made it a constant dictionary).

I'm sure I'm just missing something simple.

Thomas Thomas
Thomas Thomas
1,706 Points

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

let ukCurrency = currencies.removeValueForKey("UK")

Thomas Thomas
Thomas Thomas
1,706 Points

Sorry Vittorio, I was using () instead of []

Thank you so much for your help!!! You fixed about an hour of frustration!!