Loop Through Dictionary in Swift

In this very short Swift tutorial, you will learn how to loop or iterate over a dictionary in Swift and print out key/value pairs.

Loop Through Dictionary

// Create a Dictionary with two elements
var myDictionary = ["first_name": "Sergey", "last_name": "Kargopolov"]

// Loop through dictionary and print key/value pair
for (key,value) in myDictionary {
    print("\(key) = \(value)")
}

If you run the above code in Xcode playground you will get the following result:

last_name = Kargopolov
first_name = Sergey

Below is an image of my Xcode Playground with the above code snippet executed.

I hope you find this short Swift code snippet helpful.


Leave a Reply

Your email address will not be published. Required fields are marked *