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!

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

Eduardo Calvachi
Eduardo Calvachi
6,049 Points

Quiz Question about equivalent ways of updating Dictionary

One of the quiz a question that comes up is wether the following statements are equivalent:

dict.updateValue("yetAnotherValue", forKey: 3)
dict[3] = "yetAnotherValue"

I would like to verify the reason why they are not equivalent,

They accomplish the same task, but have a different in implementation. I tested the return types on Xcode.

Is it because using updateValue method returns an optional(nil in this case) and adding an entry with subString notation just returns the String Value?


1 Answer

Hi Eduardo,

Per the Swift documentation, "Unlike a subscript... the updateValue(_:forKey:) method returns the old value after performing an update. This enables you to check whether or not an update took place."

Hope that helps!

Eduardo Calvachi
Eduardo Calvachi
6,049 Points

Alright, thanks for pointing out the difference!