
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Iterating Through a Dictionary in Swift
We can iterate through a dictionary using various methods in swift. You can iterate a dictionary for keys and values also.
We will use the different approaches to iterate a dictionary like below ?
Using a for-in loop
Iterating all the keys in a dictionary
Iterating all the values in a dictionary
Iterating all the elements using the enumerated() method
Using a for-in loop
Most of the time we use the for-in loop to iterate through a dictionary. Using a for-in loop, you can iterate all the elements of a dictionary like the below ?
Syntax
for (key, value) in dictionary { }
In the syntax, where key and value is the names of the variables that will hold the current key and value, respectively.
Example
import Foundation let colorsDictionary = [ "aliceblue": "#f0f8ff", "antiquewhite": "#faebd7", "aqua": "#00ffff", "aquamarine": "#7fffd4", "azure": "#f0ffff", "beige": "#f5f5dc", "bisque": "#ffe4c4", "black": "#000000", "blanchedalmond": "#ffebcd", "blue": "#0000ff", "blueviolet": "#8a2be2", "brown": "#a52a2a" ] for (colorName, colorValue) in colorsDictionary { print("Name: \(colorName) and its hex code: \(colorValue)") }
Output
Name: brown and its hex code: #a52a2a Name: bisque and its hex code: #ffe4c4 Name: blueviolet and its hex code: #8a2be2 Name: aqua and its hex code: #00ffff Name: aliceblue and its hex code: #f0f8ff Name: aquamarine and its hex code: #7fffd4 Name: azure and its hex code: #f0ffff Name: beige and its hex code: #f5f5dc Name: blanchedalmond and its hex code: #ffebcd Name: blue and its hex code: #0000ff Name: antiquewhite and its hex code: #faebd7 Name: black and its hex code: #000000
In the above example, we created a dictionary to define some colors with names and hex values. Using a for-in loop, iterating all the elements one by one in an unspecified order. It gives the element in the pair (key, value) format.
Iterating through all the keys
If you want to access or iterate all the keys at once, you can do that using the "keys" property. This property returns an array of strings type which gives you all the keys of a dictionary.
Example
import Foundation let colorsDictionary = ["aliceblue": "#f0f8ff", "antiquewhite": "#faebd7", "aqua": "#00ffff", "aquamarine": "#7fffd4", "azure": "#f0ffff", "beige": "#f5f5dc", "bisque": "#ffe4c4", "black": "#000000", "blanchedalmond": "#ffebcd", "blue": "#0000ff", "blueviolet": "#8a2be2", "brown": "#a52a2a" ] for key in colorsDictionary.keys { print("Key: \(key)") }
Output
Key: brown Key: antiquewhite Key: bisque Key: beige Key: black Key: aqua Key: azure Key: aliceblue Key: blue Key: blueviolet Key: aquamarine Key: blanchedalmond
In the above example, we created a dictionary to define some colors with names and hex values. Using a for-in loop, iterating all the keys of a dictionary.
Iterating through all the values
If you want to access or iterate all the values at once, you can do that using the "values" property. This property returns an array of strings type which gives you all the values of a dictionary.
Example
import Foundation let colorsDictionary = ["aliceblue": "#f0f8ff", "antiquewhite": "#faebd7", "aqua": "#00ffff", "aquamarine": "#7fffd4", "azure": "#f0ffff", "beige": "#f5f5dc", "bisque": "#ffe4c4", "black": "#000000", "blanchedalmond": "#ffebcd", "blue": "#0000ff", "blueviolet": "#8a2be2", "brown": "#a52a2a" ] for value in colorsDictionary.values { print("Value: \(value)") }
Output
Value: #00ffff Value: #f0f8ff Value: #f5f5dc Value: #000000 Value: #f0ffff Value: #ffebcd Value: #7fffd4 Value: #ffe4c4 Value: #0000ff Value: #faebd7 Value: #a52a2a Value: #8a2be2
Conclusion
We can iterate the elements, keys, and values of a dictionary in Swift. The dictionary provides the "keys" property to access an array of all the keys. In the same way, a property called "values" gives you an array of all the values.
You can iterate through a dictionary using a for-in loop and other approaches. But remember that, the order of elements is unspecified. But still using the enumerated() function, you can keep track of an index of elements while accessing it.