Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?

Thanks

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!