In Swift, a dictionary is a data structure that stores information in key-value pairs. You can iterate or loop through a dictionary to access the keys and values within the loop. There are several ways to achieve this, including using for-in
loops and forEach
methods.
If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App
Using a For-In loop
The for-in loop is a simpler way to iterate through a dictionary. You can use it to access both keys and values without explicitly defining them:
var authorsName = [ 134: "Sergey", 143: "Kargopolov", 187: "Ferdous", 156: "Edwin", 164: "Cardenas", ] for author in authorsName { print("\(author.key), \(author.value)") }
Output:
143, Kargopolov 156, Edwin 187, Ferdous 134, Sergey 164, Cardenas
This example author
represents each key-value pair in the dictionary. The key
and value
properties of each pair are accessed using dot notation. Can you notice that it prints the authors’ names in a different order than specified?
Remember, Swift dictionaries are unordered collections, so the order of keys and values may not be consistent across iterations.
You can use another type of for-in
loop to iterate over a dictionary in Swift and access each key-value pair.
var authorsName = [ 134: "Sergey", 143: "Kargopolov", 187: "Ferdous", 156: "Edwin", 164: "Cardenas", ] for (key, value) in authorsName { print("\(key), \(value)") }
Output:
187, Ferdous 134, Sergey 164, Cardenas 143, Kargopolov 156, Edwin
In this code, key
and value
represent each key-value pair in the dictionary. The print
function is then used to display each pair.
Using forEach method
The forEach method provides another way to iterate through a dictionary. It allows you to specify a closure that is executed for each key-value pair in the dictionary:
var authorsName = [ 134: "Sergey", 143: "Kargopolov", 187: "Ferdous", 156: "Edwin", 164: "Cardenas", ] authorsName.forEach { id, name in print("\(id), \(name)") } // Output 187, Ferdous 134, Sergey 164, Cardenas 156, Edwin 143, Kargopolov
In this example, id
and name
represent each key-value pair in the dictionary. The print
function is then used to display each pair.
Iterating through keys only
Sometimes, you might want to iterate through the keys of a dictionary only. You can achieve this by using the keys
property of the dictionary:
var authorsName = [ 134: "Sergey", 143: "Kargopolov", 187: "Ferdous", 156: "Edwin", 164: "Cardenas", ] for key in authorsName.keys { print("Key: \(key)") }
Output:
Key: 156 Key: 164 Key: 143 Key: 134 Key: 187
In this example, key
represents each key in the dictionary. The print
function is then used to display each key.
Iterating through values only
Similarly, you can iterate through all the values of a dictionary using the values
property:
var authorsName = [ 134: "Sergey", 143: "Kargopolov", 187: "Ferdous", 156: "Edwin", 164: "Cardenas", ] for value in authorsName.values { print("Value: \(value)") }
Output:
Value: Sergey Value: Kargopolove Value: Cardenas Value: Ferdous Value: Edwin
In this example, value
represents each value in the dictionary. The print
function is then used to display each value.
Conclusion
Iterating through a dictionary in Swift can be achieved using various methods such as for-in
loops and forEach
methods. These methods allow you to access keys and values in a dictionary, providing flexibility depending on your specific needs.
To find more code examples, please check Swift Code Examples page.
If you are interested in video lessons on how to write Unit tests and UI tests to test your Swift mobile app, check out this page: Unit Testing Swift Mobile App