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?

David Petrin
David Petrin
3,384 Points

What is wrong with my second line of code?

What is wrong with my second line of code?

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

2 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hi David,

Because you used let to declare your dictionary, it's a constant, and you can't use removeValueForKey on it. Change let currencies to var currencies and it would work. However, there's no reason to use removeValueForKey in this case - you don't need to change the dictionary. Instead, just index into it and grab the value for "UK".

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

Best,

Greg

David Petrin
David Petrin
3,384 Points

Thanks for the help Greg!