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 Collections and Control Flow Dictionaries in Swift Modifying a Dictionary

Sebastian Eguez
Sebastian Eguez
8,248 Points

What 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


// 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.updateValue("La Guardia Airport", forKey: "LGA")

What is the difference between changing "LGA": "La Guardia" using

airportCodes["LGA"] = "La Guardia International Airport"


airportCodes.updateValue("La Guardia Airport", forKey: "LGA")


Why and when should you use one or the other?


1 Answer

Matt Skelton
Matt Skelton
4,548 Points

Hey 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!

8,069 Points

The 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?