How to iterate for loop in reverse order in Swift?


In Swift, there are different approaches to iterating a for loop in reverse order. In this article, you will see some methods like reverse, ranges, stride(from:to:by:), forEach(), etc. Each method can be used in different use cases. Also, you should know that each method has its own performance.

Using the reversed() Method

This method returns a new array by reversing the elements of an array. It does not change the order of the input array.

Step 1 − Create an input array to iterate

Step 2 − Perform a for loop with the reversed() function

Step 3 − Perform an action on each element of an array inside the for-loop body

Example

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers.reversed() {
   print("Number is \(number)")
}

Output

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Using the reverse() Method

This method reverses the elements of the input array. In order to make the array reversible, you have to make it mutable using the var keyword.

Step 1 − Create a mutable input array to iterate

Step 2 − Call the reverse() function to reverse the array elements

Step 3 − Perform an action on each element of an array inside the for-loop body

Example

var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.reverse()
for number in numbers {
   print("Number is \(number)")
}

Output

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Using the Stride(from:to:by:) Function

If you want to iterate over a range in reverse order, you can use the stride(from:to:by:) method with a negative stride. In this approach, you don't need to call any reverse method on the input array.

Step 1 − Call stride() function with predefine range

Step 2 − Perform an action on each element of an array inside the for-loop body

Example

for number in stride(from: 10, to: 0, by: -2) {
   print("Number is \(number)")
}

Output

Number is 10
Number is 8
Number is 6
Number is 4
Number is 2

Using the forEach() Function

You can use the forEach function to iterate an array in reverse order. The forEach is a high-order function in the Swift language.

Step 1 − Create an input array to iterate

Step 2 − Call the reversed() to input’s array along with forEach function to iterate the array elements

Step 3 − Perform an action on each element of an array inside the for-loop body

Example

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers.reversed().forEach { number in
   print("Number is \(number)")
}

Output

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Using a for Loop with the Indices Property of an Array

You can reverse the indices of the array elements rather than trying to reverse them directly. This property provides a flexible way to iterate an array in reverse order.

Step 1 − Create a mutable input array to iterate

Step 2 − Call the reverse() function to indices property on the array elements

Step 3 − Perform an action on each element of an array inside the for-loop body

Example

let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for index in numbers.indices.reversed() {
    print("Number is \(numbers[index])")
}

Output

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Using the reversed() Method with a Range

You can define a range to iterate in a loop. Using the reversed() function, you can iterate the range in reverse order.

Step 1 − Create a range with start and end values

Step 2 − Call the reverse() function to reverse the range values

Step 3 − Perform an action on each element of an array inside the for-loop body

Example

for number in (1...10).reversed() {
   print("Number is \(number)")
}

Output

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

Using the enumerated() Method with the reversed() Method

You can perform the enumerated() method to access the pair of index and element from an array along with the reversed() function.

Step 1 − Create a mutable input array to iterate

Step 2 − Call the reverse() function to enumerated values to reverse the array elements

Step 3 − Perform an action on each element and index of an array inside the for-loop body

Example

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

Output

Index: 9 and number: 10
Index: 8 and number: 9
Index: 7 and number: 8
Index: 6 and number: 7
Index: 5 and number: 6
Index: 4 and number: 5
Index: 3 and number: 4
Index: 2 and number: 3
Index: 1 and number: 2
Index: 0 and number: 1

Conclusion

In Swift, you have different methods to iterate a for loop in reverse order. Each method can use be used in different conditions according to your requirement. There are different methods you can use to iterate a for loop −

  • Using reversed() method

  • Using reverse() method

  • By combining the enumerated() method with the reversed()

  • Using indices property

You can choose any method based on your requirement and use cases. Because each method can be used for different cases.

Updated on: 04-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements