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?

Roderick Royce
Roderick Royce
1,236 Points

Which words do use to reassign a key to a dictionary?

I have created a dictionary of currencies but how do I change a key connected to the key's pair? It started as UK: pounds and now I need to change it to UK: ukCurrency.

Thank you.

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

let ukCurrency = currencies.removeValueForKey("UK")
Roderick Royce
Roderick Royce
1,236 Points

I tried the following you gave and it looked as if it should work but it says to create the constant for the 1st challenge and to keep it constant for the 2nd challenge although it won't work in the real Xcode and it is currently not showing anything more today so I emailed the "contact us" and I hope they can answer and give me the correct code.

Thank you,

R

Philip Ondrejack
Philip Ondrejack
4,287 Points

No problem dude. I used Playground to check my answer so it might be different than what's on Treehouse.

Keep at it. It'll feel good when you get it.

3 Answers

Philip Ondrejack
Philip Ondrejack
4,287 Points

Remember when assigning variables that there are two types, mutable and immutable. When you use let you cannot(immutable) change the variable. Instead try using var.

You're outside the scope of this course with removeValueForKey. Instead try working with basic assignment like currencies["UK"].

Roderick Royce
Roderick Royce
1,236 Points

assignment: Assign the value for key "UK" to a constant named ukCurrency. Here is my work and the 5th line is still wrong:

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

currencies["UK"] = "ukCurrency"

! Bummer You need have let ukCurrency defined. It told me to put the keys and values of the dictionary as constants and the course did tell me to use the let item = currencies.removeValueForKey("UK") but it doesn't want to work on my xCode software. Thanks for your help.

Philip Ondrejack
Philip Ondrejack
4,287 Points

Give this a shot...

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

let currency = "UKCurrency"

currencies.removeValueForKey("UK")

currencies["UK"] = currency