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.

Sebastian Eguez
8,248 PointsWhat is the difference between updateValue and...
// Dictionaries
/*
Airport Code (Key) Airport Name (Value)
LGA La Guardia
LHR Heathrow
CDG Charles de Gaulle
HKG Hong Kong International
DXB Dubai International
*/
// ["LGA": "La Guardia", "LHR": "Heathrow", "CDG": "Charles de Gaulle", "HKG": "Hong Kong International", "DXB": "Dubai International" // You can also write this like BELOW
var airportCodes: [String: String] =
[
"LGA": "La Guardia",
"LHR": "Heathrow",
"CDG": "Charles de Gaulle",
"HKG": "Hong Kong International",
"DXB": "Dubai International"
]
let currentWeather = ["Temperature": 75.0] // Press the option button and hover over currentWeather
// Reading from a dictionary
airportCodes["LGA"]
airportCodes["HKG"]
// Inserting Key Value Pairs
airportCodes["SYD"] = "Sydney Airport" // airportCodes needs to be a var to insert a key value pair
airportCodes["LGA"] = "La Guardia International Airport"
airportCodes["LGA"]
airportCodes.updateValue("La Guardia Airport", forKey: "LGA")
What is the difference between changing "LGA": "La Guardia" using
airportCodes["LGA"] = "La Guardia International Airport"
and
airportCodes.updateValue("La Guardia Airport", forKey: "LGA")
?
Why and when should you use one or the other?
Thanks.
1 Answer

Matt Skelton
4,548 PointsHey Sebastian,
I started typing an explanation for the advantages of using each, and then I stumbled upon this in the Apple documentation.
Take a look at the section under "Accessing and Modifying a Dictionary", it covers your question with examples and describes it much better than I could!
Sebastian Eguez
8,248 PointsSebastian Eguez
8,248 PointsThanks.
mrtroy
8,069 Pointsmrtroy
8,069 PointsThe Apple documentation did a good job of explaining how to use the Dictionary but I am still wondering if the method way is better then an assignment. It does have more options but does it conserve CPU time or space?