Swift forEach method (forEach loop)


In Swift, the forEach is a dedicated method specially provided to loop through each element of an array, of a dictionary or of a set. Using this method, you can get each item from a collection.

You can also use this method to loop within a range by setting a start value and an end value.

The forEach method, calls the given closure of each item in the sequence in the same order as you can see in the for-in loop.

Using forEach to loop over an array items

Now let’s see a simple example where we will print each name of students from an array:

var studentNames = ["Jennifer", "Edward", "Margaret", "Walter", "Stephanie", "Robert"]

studentNames.forEach {
    print($0)
}

The output of the above program will be:

Jennifer
Edward
Margaret
Walter
Stephanie
Robert

In the above code, we store some student names in an array. After that, we run the forEach method to loop through the studentNames array and print the names.

Here the $0 is a shortcut that represents the first argument in the array. If there was a second shorthand argument, then we could represent it with $1. But here we don’t need to use $1 as there is only one argument.

We can perform the same task in another way using the same forEach loop. It is done by using the forEach in a different way like you can see below:

var studentNames = ["Jennifer", "Edward", "Margaret", "Walter", "Stephanie", "Robert"]

// Here we are passing an explicit argument
studentNames.forEach { student in
    print(student)
}

The output of this code is the same as you can see in the previous code. In this example, we have used the explicit argument instead of using $0 in the forEach loop and using it to represent each item of the array.

Loop through a dictionary

Using the forEach method, you can also loop through a dictionary to get each key-value pair of the dictionary.

Now see an example below:

var weatherData = [
    "Temperature": "25.7",
    "Humidity": "79%",
    "Wind Speedy": "23km/hour",
    "Condition": "Light Rain"
]

weatherData.forEach {
    print("\($0.key): \($0.value)")
}

And the output will be:

Humidity: 79%
Condition: Light Rain
Temperature: 25.7
Wind Speedy: 23km/hour

You can also provide an explicit argument for dictionary item in the Swift forEach loop:

var weatherData = [
    "Temperature": "25.7",
    "Humidity": "79%",
    "Wind Speedy": "23km/hour",
    "Condition": "Light Rain"
]

weatherData.forEach { data in
    print("\(data.key): \(data.value)")
}

The above code will print the same as you can see in the previous code.

Loop over a set in Swift using forEach method

Below is the code example in Swift to loop through a set items where we are printing each of the element values from the Swift set:

var colorSet: Set = ["Blue", "Green", "Red", "Purple"]

colorSet.forEach {
    print($0)
}

The output will be:

Green
Red
Blue
Purple

forEach method to create a closed range

Using the Swift forEach method, you can create a closed range of values using the ... operator. Below is the syntax:

(START…END).forEach {
// Some code
}

Where START is the starting value where the range will start and END is the value where the range will end.

Let’s see with an example:

(0...7).forEach {
    print($0)
}

The output will be:

0
1
2
3
4
5
6
7

Using the explicit argument, the code will be:

(0...7).forEach { number in
    print(number)
}

And the output will be as same as above.

Below is another example of range using the forEach loop where we are printing items up to less than 7:

(0..<7).forEach {
    print($0)
}

Output:

0
1
2
3
4
5
6

You can see that, this time the end value of the range is 6 as we have used ..< operator with forEach. This type of range is called the half-open range.


There are many more usages of forEach loop that can be done in Swift. The examples that you have learned are most common in Swift programming.