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

Updating a Dictionary

Good day gents, when using an updateValue to update a value within a dictionary ... how to update more then one item within a single line ? can it be done ? or only one item per update ?

2 Answers

The updateValue(_:forKey:) method only takes two parameters, one value and one key. So you cannot update more than one value in your dictionary using the updateValue method. I don't think any method exists that can be called on by a dictionary to update more than one of it's value's within a single method declaration.

Not sure about your exact use case, but updateValue method only lets you change one key/value pair at a time.

If you were applying the same change to all your dictionary values (e.g. making them all uppercase or appending a string), you could use a for-loop.

var myDict = [
    "SFO": "San Francisco",
    "LAX": "Los Angeles",
    "DFW": "Dallas Fort Worth"
]

for (key, value) in myDict {
    myDict.updateValue(value + " International Airport", forKey: key)
}

Thanks for answering ... but actually my question was if i can update more than one key/value at a time within a dictionary, not necessary making them upper case or lower case .... it's about changing the content of a specific key/value.