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

Difference between myDictionary.updateValue("yetAnotherValue", forKey: 3) and myDictionary[3] = "yetAnotherValue".

What is the difference between these two. They seem to both create or update the key value pair of myDictionary.

1 Answer

Hey Drew Butcher,

You are correct, both are valid ways to update and create values in your dictionary.

// Here you are creating a key,value pair directly
myDictionary[3] = "yetAnotherValue"

// Here you are using the updateValue method
myDictionary.updateValue("yetAnotherValue", forKey: 3)

//It is safer to interact with your data via methods, compared to manipulating them directly.

Good Luck

Cool, so it's safer to use the method call, but which is faster?