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 2.0 Collections and Control Flow Dictionaries Recap: Dictionaries

Andreea Onu
Andreea Onu
2,167 Points

The right answer on this question seems to be "True", but not the "False" as you indicate. Please explain

both expressions create a new dictionary entry for key = 3, in case it does not exist ... otherwise key's value is updated when exactly these 2 expressions are not equivalent?

1 Answer

Michael Reining
Michael Reining
10,101 Points

Hi Andrea,

The only acceptable answer is:

We add a third key value pair to the dictionary.

Why?

Because at the very top it says:

// Given the following code:

var dict = [1: "someValue", 2: "anotherValue"]
dict.updateValue("yetAnoterValue", forKey: 3) // add third item to dictionary

for (key, value) in dict { // I added this to see the results in the playground
    print(value)
}

If a 3rd value already existed, then it would update the value but given the specific code in the challenge, no third value existed and so only one answer is correct.

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Now you can practice writing Swift code directly on your iPhone :-)

Code! Learn how to program with Swift

PPS: I would recommend to always play with examples in the Playground to get a feel for how to work with the code and see how it responds when you make changes.

Andreea Onu
Andreea Onu
2,167 Points

Thanks for the fast answer Mike, just your answer is not really related to my question (instead, it seems to be related to another quiz question). Anyway, my question was for this quiz:

The following code snippets are equivalent dict.updateValue("yetAnotherValue", forKey: 3) and dict[3] = "yetAnotherValue"

I say that snippets are equivalent but you say that they aren't. Just wanna know why. Thank you!!!

Michael Reining
Michael Reining
10,101 Points

Hi Andrea,

Sorry, I did not get the same question in my review so I was confused.

I would agree with you and say that the two statements are equivalent so if that was not the accepted answer, I am not sure why.

Mike