Skip to content
DeveloperMemos

Using Swift's enumerated() to Loop With an Index

Swift, Enumerated, Looping2 min read

Swift provides a powerful and convenient way to iterate over collections with the help of the enumerated() method. This method allows you to loop through a collection while also accessing the index of each element. In this article, we will explore how to use Swift's enumerated() method to loop with an index, along with some practical examples.

Understanding enumerated()

The enumerated() method is a versatile tool that combines the elements of a collection with their corresponding indices. It returns a sequence of tuples, where each tuple consists of an index-value pair. This allows you to access both the index and the value of each element during iteration.

Here's the general syntax of the enumerated() method:

1for (index, element) in collection.enumerated() {
2 // Code block
3}

In the code above, collection represents the collection you want to iterate over, index holds the index of the current element, and element represents the value of the current element.

Example 1: Looping through an Array

Let's start with a simple example of looping through an array using enumerated(). Consider the following array:

1let fruits = ["Apple", "Banana", "Orange", "Mango"]

To iterate over this array while accessing the index and value of each element, we can use the enumerated() method as shown below:

1for (index, fruit) in fruits.enumerated() {
2 print("Index: \(index), Fruit: \(fruit)")
3}

The output of the above code will be:

1Index: 0, Fruit: Apple
2Index: 1, Fruit: Banana
3Index: 2, Fruit: Orange
4Index: 3, Fruit: Mango

By utilizing enumerated(), we were able to access both the index and the value of each fruit in the array.

Example 2: Enumerating a Dictionary

In addition to arrays, you can also use enumerated() to iterate over dictionaries. However, since dictionaries are unordered collections, the order of elements during iteration may not follow the insertion order.

Consider the following dictionary that maps country codes to their corresponding names:

1let countryCodes = [1: "USA", 44: "UK", 33: "France", 49: "Germany"]

To loop through this dictionary while accessing the index and value of each element, we can leverage the enumerated() method as shown below:

1for (index, (code, country)) in countryCodes.enumerated() {
2 print("Index: \(index), Code: \(code), Country: \(country)")
3}

The output of the above code will be:

1Index: 0, Code: 33, Country: France
2Index: 1, Code: 44, Country: UK
3Index: 2, Code: 49, Country: Germany
4Index: 3, Code: 1, Country: USA

As you can see, even though the order of elements doesn't match the original dictionary, the enumerated() method provides us with the index and the corresponding key and value pairs, allowing us to perform operations that require both.

Example 3: Filtering Elements with Index

Another advantage of using enumerated() is the ability to filter elements based on their index. Let's say we have an array of numbers, and we want to print only the even numbers along with their indices. We can achieve this using enumerated() along with a conditional statement:

1let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
2
3for (index, number) in numbers.enumerated() {
4 if number % 2 == 0 {
5 print("Index: \(index), Number: \(number)")
6 }
7}

The output of the above code will be:

1Index: 1, Number: 2
2Index: 3, Number: 4
3Index: 5, Number: 6
4Index: 7, Number: 8
5Index: 9, Number: 10

By checking the condition number % 2 == 0, we ensure that only even numbers are printed along with their corresponding indices.

Conclusion

Swift's enumerated() method is a powerful tool for looping through collections while keeping track of the index. By leveraging this method, you can conveniently access both the index and the value of each element, enabling you to perform various operations based on the index or the combination of index and value. Whether you're working with arrays, dictionaries, or other collections, enumerated() provides a concise and efficient way to iterate with an index.

In this article, we explored the basics of using Swift's enumerated() method, along with practical examples of looping through arrays, dictionaries, and filtering elements based on the index. By understanding and utilizing this method effectively, you can enhance your Swift code and make it more expressive and readable!