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?

If I made the dictionary "currencies" a constant, how can I then change one of the keys later on?

If you look at my code, the second line is what im trying to do but am unable to.

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

currencies["UK"] = "ukCurrency""

1 Answer

Stone Preston
Stone Preston
42,016 Points

you misinterpreted what the task is asking you to do.

task 2 states: Assign the value for key "UK" to a constant named ukCurrency.

you dont change the value assigned to the key "UK", you assign the value of that key to a new constant:

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

let ukCurrency = currencies["UK"] // ukCurrency now has a value of "Pound"
// currencies remains unchanged, with a value of [ "US": "Dollar", "UK": "Pound", "JP": "Yen"]

this does not modify the currencies dict at all, you just access a value from it. nothing in the dictionary changes. you are basically saying "give me the value that corresponds to this key, and assign that value to this constant". it doesnt change anything about the dictionary itself