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?

COnfused

Why in the challenge must it be let ukCurrency= currencies ["UK"]

I dont understand why we have to declare currencies before UK when he does not do that in the video

6 Answers

Hi Hunter,

From memory, in this challenge we're creating a dictionary which is called currencies. A dictionary holds key:value pairs. This dictionary is something like:

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

So, you then need to create another constant called ukCurrency that contains the value associated with the key UK. That's a double step. So, let's define the constant then worry about the next bit:

let ukCurrency = ...

So, what's on the right of this? We need to access the dictionary and get the value associated to the key, "UK". Just like in an array, you can access elements by it's key. In an array, the index is the position in the array ... array[0], array[1], array[2] etc. That's great, it is an ordered list arranged by order of the index.

In the dictionary, there's no order; the values are assigned to their keys. That can be accessed in exactly the same way. dict["key"], dict["anotherKey"], dict["oneMoreKey"].

Here, the key is "UK" and the dictionary is called currencies. So, to complete the partial line above, it looks like:

let ukCurrency = currencies["UK"]

I hope that makes sense, else give me a shout back here, or catch me on Twitter; @OnlySteveH

Steve.

John Stephanites
John Stephanites
2,007 Points

I did this and it still says it's wrong! I went through multiple different scenarios but ran it in the Playground and it worked but will not on your editor. I found this to be true in the last module as well. What do I do now?

I thought I'd already replied to this, but clearly not!

Running stuff in a Playground isn't a way to test if your code will pass the challenge. The playground has no idea what you're trying to do so it only serves as a test to check your code is syntactically correct - it has no idea what question you're answering.

So, "What do I do now?" - post your code and the question you're trying to answer and also the error that the online compiler gives you. With all that we should be able to figure out the problem.

Steve.

John Stephanites
John Stephanites
2,007 Points

I do not understand what you mean about the Playground. If I am writing what the Editor has then isn't that the same thing. The Editor only knows what you tell it as well. Isn't that what the Playground is essentially an Editor just like on Treehouse? If not, what good would it be then?

Hi John,

The Treehouse web app 'knows' what output it is expecting from the code - it runs tests against your code and ensures that your code behaves as expected. The Xcode playground does not - it is just a code editor that knows what Swift expects, and applies those rules to your input. So, the PLayground makes sure what you type is correct from a Swift perspetive, the Treehouse app does that too, but also applies its knowledge of the desired outcome to test your code that stage further.

So, the payground will say that this code is fine:

class Temperature {
    var celsius: Float = 0.0
    var fahrenheit: Float {
        get {
            return (celsius * 1.8) + 32
        }
        set {
            celsius = (newValue - 32) / 1.8
        }
    }
    init(celsius: Float) {
        self.celsius = celsius
    }
}

That's because it is fine. But it won't pass the challenge you're struggling with.

If you can paste your code in, we can see what's up with it and figure out a fix.

Steve.

John Stephanites
John Stephanites
2,007 Points

I did end up passing the challenge but that is the key point I was getting confused with. The Editor has the answer it wants from you and will not accept code that may work but wouldn't be the best way it should be done pertaining to said situation. I was running the code in Playground first and then when I thought it was right I was entering it in the Editor. I now realize there are multiple ways to get to a certain outcome but the Editor knows the situation and what it wants. I appreciate your explanation. Thank you for explaining it in depth to this newb!