
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
Print Keys and Values of a Dictionary in Swift
A dictionary is an unordered collection in which data is stored in the form of key-value pairs. To print the keys and values of a dictionary Swift supports the following methods ?
Using for-in loop
Using properties
Method 1: Using for-in Loop
To print keys and values of the given dictionary we can use a for-in loop. The for-in loop iterates through each pair of the dictionary and displays them on the output screen.
Syntax
for(key, value) in dict{ print("\(key) = \(value)") }
Here, the key represents the key and value represents the associated value of the current key and the dict represents the dictionary.
Example
In the following Swift program, we will print a dictionary. So, we will create a dictionary with key-value pairs. Then we use the for-in loop, which iterates over the key-value pairs present in the given dictionary and displays them on the output screen in key:value format.
import Foundation import Glibc // Create a dictionary let myDict = ["i": 234, "ii": 782, "iii": 786, "ix":8, "x": 19] // Printing key-value pairs using for-in loop for(mKey, mValue) in myDict { print("\(mKey) -> \(mValue)") }
Output
i -> 234 ix -> 8 ii -> 782 iii -> 786 x -> 19
Method 2: Using Properties
To print keys and values of the given dictionary Swift provides the following in-built properties ?
keys ? This property returns a collection which contains only keys.
Syntax
for x in Dict.keys { // statements }
Here Dict is the dictionary from which we are going to take keys.
values ? This property returns a collection which contains only values.
Syntax
for x in Dict.values { // statements }
Here Dict is the dictionary from which we are going to take values.
Example
In the following Swift program, we will print the keys and values of a dictionary. So, first, we create a dictionary with key-value pairs. Then we use the for-in loop along with the keys property to iterate over the keys of the dictionary and display all the keys present in the given dictionary. Then we again use the for-in loop with value property to iterate over the values of the dictionary and display all the values present in the given dictionary.
import Foundation import Glibc // Create a dictionary let myDict = ["i": 234, "ii": 782, "iii": 786, "ix":8, "x": 19] // Printing keys print("Keys:") for m in myDict.keys { print("\(m)") } // Printing values print("\nValues:") for n in myDict.values { print("\(n)") }
Output
Keys: ii i ix x iii Values: 782 234 8 19 786
Conclusion
So this is how we can create and print the keys and values of a dictionary. Both methods print keys and values of the given dictionary. If you want to print keys and values separately you can use keys and values properties whereas if you want to display both key and value pairs together, then you can use a for-in loop. Also, note that the order of the keys and values or key-value pairs may not be the same as given in the dictionary because dictionaries are unordered collections.