.updateValue()
Published May 28, 2023
Contribute to Docs
The .updateValue()
method replaces the value stored in a dictionary for a given key, and returns the replaced value. If the key does not exist, a new key-value pair is created and nil
is returned.
Syntax
dictionary.updateValue(value, forKey: key)
value
is the value being updated or added to the dictionary.key
is the key getting updated or created.
Examples
In the example below, the key Loki
is found and updated to the new value, 8
. In addition, the key Lenny
did not previously exist; a new key-value pair is added to the dictionary:
var petAges = ["Aurora": 10, "Loki": 7]// Update existing key's valuepetAges.updateValue(8, forKey: "Loki")// Add new key-value pairpetAges.updateValue(12, forKey: "Lenny")print(petAges)
This will output:
["Loki": 8, "Lenny": 12, "Aurora": 11]
Return Values
.updateValue()
returns the original value if an existing key is replaced.
The key Dogs
exists in the dictionary below, and its value is updated:
var pets = ["Dogs": 1, "Cats": 1]if let originalValue = pets.updateValue(2, forKey: "Dogs") {print("The original value of \(originalValue) was updated.")} else {print("Key was not found in dictionary.")}
This will output:
The original value of 1 was replaced.
If a key does not exist, .updateValue()
returns nil
.
Parrots
does not exist as a key and nil
is returned, therefore the else
block is run:
if let originalValue = pets.updateValue(2, forKey: "Parrots") {print("The original value of \(originalValue) was updated.")} else {print("Key was not found in dictionary.")}
This will output:
Key was not found in dictionary.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.