Swift Dictionary - Creating, Updating, and Adding Key-Value Pairs
In this article, we will explain the basics of Swift Dictionary, how to update values, and how to add key-value pairs.
The Basics of Swift Dictionaries
A Swift Dictionary is an unordered collection of data stored as key-value pairs.
You can add, remove, and access values by specifying keys. Since each key must be unique, a Dictionary cannot contain duplicate keys.
Dictionary keys are case-sensitive and must conform to Hashable, just like Set elements.
Basic Swift types such as String, Int, and Double are Hashable.
There are several ways to create a Dictionary in Swift.
For example, to create an empty Dictionary with String keys and Int values, you can use any of the following:
var dict1:[String: Int] = [:]
var dict2 = [String: Int]()
var dict3: Dictionary<String, Int> = [:]
var dict4 = Dictionary<String, Int>()
All of these create an empty Dictionary of the same type. According to Swift's official documentation, the first or second style using [Key: Value] is recommended.
As with other variable definitions, you can also initialize a Dictionary by assigning values without explicitly specifying the type:
var dict = ["Key1": 10, "Key2": 5, "Key3": 7, "Key4": 10]
If the Dictionary does not need to be modified, use let instead of var:
let dict = ["Key1": 10, "Key2": 5, "Key3": 7, "Key4": 10]
If the values have different types depending on the key, declare the Dictionary with Any:
var dict1:[String: Any] = [:]
When using Any for values, you cannot omit the type when initializing with values:
var student:[String : Any] = ["Name": "Suzuki", "Age": 15, "Gender": "M"]
Now let's start working with Swift Dictionaries.
Accessing Values in a Dictionary
To access a value for a specific key in a Swift Dictionary, use square brackets [ ] with the key.
For example, to get the value for the key Key1 in a Dictionary named dict, you can write the code as follows.
The returned value is optional, so here we specify "Not Found" as a default value.
let dict = ["Key1": "value1", "Key2": "value2", "Key3": "value3"]
print(dict["Key1"] ?? "Not Found")
The output is as follows. The value corresponding to the key Key1, which is value1, is printed.
value1
Updating Values in a Dictionary
To update a value in a Swift Dictionary, use square brackets [ ] with the key and assign a new value.
If the value type is not Any, assigning a value of a different type will result in an error.
For example, to change the value for the key Key1 to value11, you can write the code as follows.
Since we are modifying the Dictionary, the definition uses var instead of let.
var dict = ["Key1": "value1", "Key2": "value2", "Key3": "value3"]
print(dict)
dict["Key1"] = "value11"
print(dict)
The output is as follows. The value for Key1 has been updated to "value11".
["Key1": "value1", "Key2": "value2", "Key3": "value3"]
["Key1": "value11", "Key2": "value2", "Key3": "value3"]
Adding Key-Value Pairs to a Dictionary
To add a key-value pair to a Swift Dictionary, specify a new key inside square brackets [ ] and assign a value to it.
Let's define an empty Dictionary and then add three key-value pairs to it.
var dict: [String: String] = [:]
print(dict)
dict["Key1"] = "value1"
print(dict)
dict["Key2"] = "value2"
print(dict)
dict["Key3"] = "value3"
print(dict)
The output is as follows. The key-value pairs have been added to the Dictionary.
[:]
["Key1": "value1"]
["Key1": "value1", "Key2": "value2"]
["Key1": "value1", "Key2": "value2", "Key3": "value3"]
That wraps up the basics of Swift Dictionaries, how to update values, and how to add key-value pairs.