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

Jeff Bennion
Jeff Bennion
406 Points

There was a discussion months ago about this being a confusing question: is [3] an index or array. Why isn't it fixed?

Apparently the question was written assuming [3] is an index, which would make the answer false. However, it seems that it could also be a key, which would make the answer true. This question was asked 5 months ago, and I'm wondering why the question has not been fixed or removed

1 Answer

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

dict is shown to be a Dictionary in the line above it, which uses the updateValue:forKey: function, a function specifically for Dictionary types. Because of this, [3] must be a key; anything else would indicate that dict is not a dictionary, which would make the question pointless.

The question is asking whether or not updateValue:forKey: does the same thing as adding or changing a key-value pair in a Dictionary with subscripting; the answer to that question is no, they're not the same action. Subscripting just adds or changes the key-value pair; it doesn't return anything. updateValue:forKey:, on the other hand, returns the old value if the key currently exists in the Dictionary, or nil otherwise.

I hope this helps!