Swift
reverse for loop
iterate backward
Swift programming
coding techniques

How to iterate for loop in reverse order in swift?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Swift, reverse iteration depends on what you are iterating. If you are counting numbers, stride is usually the right tool. If you are walking a collection, reversed() is often cleaner and safer.

The important part is not just "go backward." It is choosing the reverse-iteration style that matches the data structure and avoids off-by-one mistakes.

Reverse Counting with stride

For numeric loops, use stride(from:through:by:) or stride(from:to:by:).

swift
for i in stride(from: 5, through: 1, by: -1) {
    print(i)
}

This prints:

  • '5'
  • '4'
  • '3'
  • '2'
  • '1'

Use through when the end value should be included. Use to when it should be excluded.

swift
for i in stride(from: 5, to: 0, by: -1) {
    print(i)
}

This also prints 5 through 1, because 0 is excluded.

Reverse Iteration Over Arrays

If you want the elements of an array in reverse order, reversed() is usually simpler than indexing manually:

swift
1let numbers = [10, 20, 30, 40]
2
3for value in numbers.reversed() {
4    print(value)
5}

This is more readable than calculating reverse indices yourself, and it avoids boundary errors.

It also makes the intent obvious to the next reader. The code says "iterate this collection backward" instead of "perform custom arithmetic on positions and hope the bounds are correct."

Reverse Iteration with Indices

Sometimes you need both the index and the element. In that case, reverse the indices:

swift
1let numbers = [10, 20, 30, 40]
2
3for index in numbers.indices.reversed() {
4    print(index, numbers[index])
5}

This is safer than writing count - 1 loops directly, because collection indices are not always plain integers in every Swift collection type.

That point matters more as soon as you move beyond arrays. Swift collections are designed around index correctness, so leaning on indices.reversed() keeps your code aligned with that model instead of hardcoding assumptions about integer offsets.

Strings and Reverse Iteration

Strings in Swift are Unicode-aware, so you should not assume integer indexing. Fortunately, reversed() still works cleanly:

swift
1let text = "Swift"
2
3for ch in text.reversed() {
4    print(ch)
5}

If you need the reversed string as a String, convert it:

swift
let reversedText = String(text.reversed())
print(reversedText)

That is safer than trying to treat a Swift String like a random-access array of bytes. Reverse iteration over characters should stay at the collection level unless you have a very specific lower-level reason not to.

When to Use Which Approach

A simple rule works well:

  • use stride for numeric countdown loops
  • use reversed() for collections and strings
  • use reversed indices only when you specifically need positions

This keeps the code expressive and prevents a lot of unnecessary index arithmetic.

That keeps the code aligned with Swift's collection model instead of forcing everything into C-style loop thinking.

Common Pitfalls

  • Using stride when you really want reversed collection elements rather than reversed numbers.
  • Mixing up to and through and getting an off-by-one error.
  • Writing manual index arithmetic for strings, which is unsafe in Swift's Unicode model.
  • Assuming reversed() always gives you a new array; it often gives a reversed view.
  • Converting to arrays unnecessarily when reversed() already solves the problem.

Summary

  • Use stride for reverse numeric loops.
  • Use reversed() for arrays, ranges, and strings when you want backward traversal.
  • Reverse collection indices only when you need positions as well as values.
  • Be careful about to versus through in stride.
  • Prefer Swift's collection APIs over manual reverse-index arithmetic.

Course illustration
Course illustration

All Rights Reserved.