Swift Dictionary - Loops, mapValues, and filter
In this article, we will explain how to loop through a Swift Dictionary and how to use the mapValues and filter methods.
Looping Through Keys and Values in a Dictionary
There are several ways to loop through the keys and values of a Swift Dictionary.
One way is to use a for-in loop.
The variable after for contains the current key-value pair. You can access the key with .key and the value with .value.
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
for d in dict {
print("\(d.key): \(d.value)")
}
The output is as follows. Since Swift Dictionaries are unordered, the printed order may differ from the order of creation, but all key-value pairs are printed.
Key3: 66
Key4: 72
Key1: 100
Key2: 30
You can also destructure the key-value pair into a tuple directly:
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
for (key, value) in dict {
print("\(key): \(value)")
}
The output is as follows:
Key4: 72
Key3: 66
Key2: 30
Key1: 100
You can also use the forEach() method to print keys and values:
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
dict.forEach { d in
print("\(d.key): \(d.value)")
}
The output is as follows. All keys and values are printed:
Key4: 72
Key1: 100
Key2: 30
Key3: 66
Unlike for-in loops, forEach() does not support break or continue.
If you need to break, continue, or return during a loop, use a for-in loop instead.
Looping Through Keys Only or Values Only
To loop only through the keys or only through the values of a Swift Dictionary, use the keys and values properties.
For example, to loop only through the keys:
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
for key in dict.keys {
print(key)
}
The output is as follows:
Key2
Key3
Key4
Key1
To loop only through the values:
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
for value in dict.values {
print(value)
}
The output is as follows:
66
72
30
100
If you want to loop through keys in sorted order and also access their values, you can use the sorted() method to sort the keys and use them to access the Dictionary:
Here, we know the values exist, so dict[key]! is used to force unwrap. Be careful when there's a possibility of nil.
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
for key in dict.keys.sorted() {
print("\(key): \(dict[key]!)")
}
The output is as follows, showing the key-value pairs in sorted key order:
Key1: 100
Key2: 30
Key3: 66
Key4: 72
Generating a Dictionary with Transformed Values Using mapValues()
In Swift, you can use the mapValues() method on a Dictionary to generate a new Dictionary with transformed values.
For example, to create a new Dictionary where all the values are multiplied by 10, you can write the code as follows.
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
let dict2 = dict.mapValues { value in
return value * 10
}
print(dict)
print(dict2)
The output is as follows. The values in dict2 have all been multiplied by 10:
["Key2": 30, "Key3": 66, "Key4": 72, "Key1": 100]
["Key2": 300, "Key3": 660, "Key4": 720, "Key1": 1000]
Generating a Dictionary with Filtered Key-Value Pairs Using filter()
In Swift, you can use the filter() method to generate a new Dictionary that contains only the key-value pairs meeting specific conditions.
For example, to create a Dictionary containing only the key-value pairs with values greater than or equal to 70, you can write the code as follows.
let dict = ["Key1": 100, "Key2": 30, "Key3": 66, "Key4": 72]
let dict2 = dict.filter { key, value in
return value >= 70
}
print(dict2)
The output is as follows. dict2 contains only the key-value pairs where the value is 70 or greater:
["Key1": 100, "Key4": 72]
That wraps up how to loop through a Swift Dictionary and how to use the mapValues and filter methods.