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

Emanuele Gorga
Emanuele Gorga
5,020 Points

update dictionary

Hello guys, I have been trying in many different ways, trying to restart XCODE too but just doesn't want to work out. When I try to use updateValue in order to update a value in the dictionary (but because doesn't find anything it just add a new value), my Xcode is giving me an error:

var airportCodes: [String: String] = ["LGA": "La Guardia", "LHR": "Heathrow", "CDG": "Charles de Gaulle", "HKG": "Hong Kong International", "DXB": "Dubai International"]

airportCodes.updateValue("Dublin Airport", forKey: DUB)

Playground execution failed:

error: SwiftBasics.playground:72:52: error: use of unresolved identifier 'DUB' airportCodes.updateValue("Dublin Airport", forKey: DUB) ^~~

Do you know how to help me out? THANK YOU

1 Answer

Jeff McDivitt
Jeff McDivitt
23,970 Points

You need the quotations around DUB because it is a string

var airportCodes: [String: String] = ["LGA": "La Guardia", "LHR": "Heathrow", "CDG": "Charles de Gaulle", "HKG": "Hong Kong International", "DXB": "Dubai International"]

airportCodes.updateValue("Dublin Airport", forKey: "DUB")
Emanuele Gorga
Emanuele Gorga
5,020 Points

yeah perfect now it works :) thank you!