Sling Academy
Home/Swift/Swift: 5 ways to iterate over a dictionary

Swift: 5 ways to iterate over a dictionary

Last updated: May 08, 2023

Using a for-in loop

In Swift, you can use a for-in loop to iterate over the key-value pairs of a dictionary. Each item in the dictionary is returned as a (key, value) tuple, and you can decompose the tuple’s members as explicitly named constants for use within the body of the loop.

Example:

// a dictionary of employees and their salaries
var employees = [
    "Andy": 5000,
    "Bobby": 4000,
    "Cosy": 3000,
    "Dondy": 4000,
    "Eva": 5500,
]

for (name, salary) in employees {
    print("\(name) has a salary of \(salary)")
}

Output:

Dondy has a salary of 4000
Andy has a salary of 5000
Bobby has a salary of 4000
Cosy has a salary of 3000
Eva has a salary of 5500

Using the forEach() method

The forEach() method can be called on a dictionary to perform a closure on each key-value pair. The closure takes a (key, value) tuple as its argument and does not return any value.

Example:

// a dictionary of employees and their salaries
var employees = [
    "Andy": 5000,
    "Bobby": 4000,
    "Cosy": 3000,
    "Dondy": 4000,
    "Eva": 5500,
]

employees.forEach { name, salary in
    print("\(name) has a salary of \(salary)")
}

Output:

Dondy has a salary of 4000
Andy has a salary of 5000
Bobby has a salary of 4000
Cosy has a salary of 3000
Eva has a salary of 5500

Using the enumerated() method

You can also use the enumerated() method on a dictionary to access its index and element as a tuple. The index is an integer that starts from zero and increases by one for each element. The element is another tuple that contains the key and value of the dictionary item.

Example:

// a dictionary of employees and their salaries
var employees = [
    "Andy": 5000,
    "Bobby": 4000,
    "Cosy": 3000,
    "Dondy": 4000,
    "Eva": 5500,
]

for (index, element) in employees.enumerated() {
    print("Item \(index): \(element)")
}

Output:

Item 0: (key: "Andy", value: 5000)
Item 1: (key: "Bobby", value: 4000)
Item 2: (key: "Dondy", value: 4000)
Item 3: (key: "Eva", value: 5500)
Item 4: (key: "Cosy", value: 3000)

Using the keys and values property

If you want to access only the keys or values of a dictionary, just use the keys or values property, respectively. These properties return a collection of the keys or values that can be iterated over with a for-in loop.

Example:

// a dictionary of employees and their salaries
var employees = [
    "Andy": 5000,
    "Bobby": 4000,
    "Cosy": 3000,
    "Dondy": 4000,
    "Eva": 5500,
]

for name in employees.keys {
    print(name)
}

Output:

Andy
Eva
Bobby
Cosy
Dondy

Using the map() method

Example:

let dict1 = [
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
]

_ = dict1.map { (key, value) in
    print("\(key) : \(value)")
}

Output:

key2 : value2
key3 : value3
key1 : value1

Next Article: Merging 2 Dictionaries in Swift

Previous Article: Swift: Check if a key exists in a dictionary

Series: Collection data types in Swift

Swift

You May Also Like

  • How to Find the Union of 2 Sets in Swift
  • How to Find the Intersection of 2 Sets in Swift
  • Subtracting 2 Sets in Swift (with Examples)
  • Swift: Removing Elements from a Set (4 Examples)
  • Swift: Checking if a Set Contains a Specific Element
  • Swift: Counting the Number of Elements in a Set
  • Adding new Elements to a Set in Swift
  • How to Create a Set in Swift
  • Swift: Converting a Dictionary into an Array
  • Merging 2 Dictionaries in Swift
  • Swift: Check if a key exists in a dictionary
  • Swift: Removing a key-value pair from a dictionary
  • Swift: Adding new key-value pairs to a dictionary
  • Swift: Counting Elements in a Dictionary
  • Swift: Ways to Calculate the Product of an Array
  • Swift: How to Convert an Array to JSON
  • Swift: Different ways to find the Min/Max of an array
  • Swift: 4 Ways to Count the Frequency of Array Elements
  • How to Compare 2 Arrays in Swift (Basic & Advanced)