Accesses the value associated with the given key for reading and writing.
- Xcode 9.0+
- Swift Standard Library
The key to find in the dictionary.
The value associated with
key is in the dictionary; otherwise,
This key-based subscript returns the value for the given key if the key is found in the dictionary, or
nil if the key is not found.
The following example creates a new dictionary and prints the value of a key found in the dictionary (
"Coral") and a key not found in the dictionary (
When you assign a value for a key and that key already exists, the dictionary overwrites the existing value. If the dictionary doesn’t contain the key, the key and value are added as a new key-value pair.
Here, the value for the key
"Coral" is updated from
18 and a new key-value pair is added for the key
If you assign
nil as the value for the given key, the dictionary removes that key and its associated value.
In the following example, the key-value pair for the key
"Aquamarine" is removed from the dictionary by assigning
nil to the key-based subscript.