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?

Mark Hockridge
Mark Hockridge
258 Points

Getting an error compiling

I'm getting an error compiling this. Having difficulty with the function removeValueForKey. What am I missing or doing wrong?

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

let ukCurrency = currencies.removeValueForKey("UK")

2 Answers

Remember that there are two keywords you can use to create identifiers: var and let. One of them creates a variable, which can be changed after it is created, and one creates a constant, which cannot be changed.

The dictionary method removeValueForKey() removes an element from the dictionary. How should you define currencies so you can add and remove values from it?

Mark Hockridge
Mark Hockridge
258 Points

thank you for your response but the first task specifically to create a constant. I then tried to change it to a variable in the next task but wouldn't let me do this.

Mark Hockridge
Mark Hockridge
258 Points

thank you for your response but the first task specifically to create a constant. I then tried to change it to a variable in the next task but wouldn't let me do this.

Mark Hockridge
Mark Hockridge
258 Points

thank you for your response but the first task specifically to create a constant. I then tried to change it to a variable in the next task but wouldn't let me do this.

Mark Hockridge
Mark Hockridge
258 Points

thank you for your response but the first task specifically to create a constant. I then tried to change it to a variable in the next task but wouldn't let me do this.

Mark Hockridge
Mark Hockridge
258 Points

thank you for your response but the first task specifically to create a constant. I then tried to change it to a variable in the next task but wouldn't let me do this.

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

In your code currencies is a constant and can not be altered.

You need to change currencies into a variable (var)

right answer:

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

let ukCurrency = currencies.removeValueForKey("UK")