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?

harleythomas
harleythomas
16,512 Points

I keep getting a compiler error because the dictionary is immutable.

I think this is happening because the dictionary is a constant, so it cannot be edited. When I change the dictionary to a variable in Xcode, the program runs fine. Unfortunately, Treehouse requires the dictionary to be defined as a constant with no explanation about how to workaround that fact. The video demonstrates the dictionary being created as a variable.

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

let ukCurrency = currencies.removeValueForKey("UK")

4 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Harley,

The challenge never asks for you to remove the key UK from the dictionary but assign it to a constant called ukCurrency which you can do by using dictionary key UK.

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

Happy coding!

harleythomas
harleythomas
16,512 Points

Thank you, Chris.

This is particular confusing because 1) This is approach is not discussed in the video even though the removeValueFromKey approach is discussed, and 2) although this concept is discussed in a previous video on arrays, the task associated with that video does ask you to remove the item from the array in order to assign it to the constant. Just some feedback.

Chris Shaw
Chris Shaw
26,676 Points

I understand that but 36 seconds in Amit says the following.

You use a key to get to its value.

Amit also does show you in the video how to select the values at around 4 minutes in.

let myDictionary =['key':'value']
//to update the dictionary you use the key to access the value since dictionaries have no index
// hence
myDictionary['key']="new value"
Chris Shaw
Chris Shaw
26,676 Points

This is incorrect.

  1. Single quotes are illegal, double quotes need to be used
  2. Constants can't be changed once they're set

oh yes you are right thinking in python

hi Chris i just thought i would give my feedback. I agree with Harley that the video does not prepare you for this challenge. Happy i foumd this thread because ive been trying to figure this out for the last two days thanks

Chris Shaw
Chris Shaw
26,676 Points

Hi Shawn,

As I said above Amit does mention this technique in the video, I would highly recommend you go back and watch it again.

http://teamtreehouse.com/library/swift-basics/collections/what-is-a-dictionary

Wow, I am impressed with the quick response time, Chris. Thank you.

I've watched the video 3 times. it might be clear to you in the video because you are an experienced developer. I am codding for the first time in my life and it was not clear. Trust me I have patience to learn new concepts so I am not just getting frustrated and commenting when something doesn't go my way. This was just my feedback. Maybe I could have listened more carefully in retrospect. Thanks again.

Chris Shaw
Chris Shaw
26,676 Points

I can understand that as I once used to feel the same way, something simple I did was instead of learning advanced concepts I went back to the basics every now and then to ensure my knowledge of programming was still fresh, in saying that I highly recommend you go through the Introduction to Programming course which uses JavaScript but creates a nice base to work with.