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 trialEduardo Calvachi
6,049 PointsQuiz 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
Evan Demaris
64,262 PointsHi 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
6,049 PointsEduardo Calvachi
6,049 PointsAlright, thanks for pointing out the difference!