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

Dictionary quiz seems to be incorrect?

Dictionary Quiz Question 2 of 5

The following code snippets are equivalent

dict.updateValue("yetAnotherValue", forKey: 3)

and

dict[3] = "yetAnotherValue"

Why is the answer not true?

5 Answers

Hi J B,

A dictionary has no order - you can't access it like an array and you can't create an additional key/value pair in that way. There's a specific method for adding and amending the value associated with each key.

Arrays retain their order; dictionaries do not so an index will only work on an array.

I hope that makes sense.

Steve.

Thank you Steve.

Is it not possible to use an integer as a key in a dictionary [Integer: String] ?

That's how I read this code, that '3' is an actual assigned key (like a customer ID number in a database), NOT the index of the value in an array (since this isn't an array, it's a dictionary).

To test this, I created a dictionary in Xcode using integer keys.

var dict = [ 1: "First Place", 2: "Second Place", 3: "Third Place"]

Xcode didn't complain about using integers as keys and both lines of code accomplished the task of updating key 3 with value "yetAnotherValue". So to me, the answer should have been True.

So is it wrong to use integers as keys in dictionaries? Or is there some other reason the answer is False?

Hi Sarah,

There's nothing wrong with using integers for keys, no. And you're absolutely correct that in the example you've created, which is the same as the question, the two dictionaries will contain the same thing after each operation.

I think the context of the quiz, and question, was assuming the index accessor [3] was implying order where none exists. I think a better example would be to utilise a dictionary where the key isn't an integer!

There is one key difference between the two ways of replacing an existing element - if it is a replacement, using the updateValue approach will return the old, replaced value for that key. Accessing the key directly with [3] doesn't; it just overwrites it.

let oldValue = dict.updatValue("new value", forKey: 3)
// oldValue now holds what *used* to be paired with the key 3
dict[3] = "another value"
// "new value" is now lost without trace!

Like I said, semantics, but a difference!

I agree that the answer to this quiz question assumes a presumption has been made and is largely incorrect despite the above. I'll drop the team a line to ask for some clarity to be added, or a correction issued.

I hope that helps.

Steve.

Thanks, Steve. I think just showing the key-value pair of the dictionary (i.e. show the key is not an integer) would clarify what the question is testing.

John Ambrose
John Ambrose
7,216 Points

Hi Steve. I just had this same question because I was thinking like Sara that based on the updateValue method, that this was a dictionary with an integer used as a key, and ended up getting it wrong. Please, can we get this question written in a slightly different way?

Yeah - it isn't a great example; I'm sorry about that. Given that the Swift library is undergoing a wholescale rewrite, this issue with go away (unless they introduce another ambiguous question!!) so there's little point in having this changed now.

Keep up the good work - and ask in the Community pages if you need help. You can @ mention me if you specifically think I can help. I'll do my best!

Steve.