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:).
This prints:
- '
5' - '
4' - '
3' - '
2' - '
1'
Use through when the end value should be included. Use to when it should be excluded.
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:
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:
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:
If you need the reversed string as a String, convert it:
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
stridefor 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
stridewhen you really want reversed collection elements rather than reversed numbers. - Mixing up
toandthroughand 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
stridefor 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
toversusthroughinstride. - Prefer Swift's collection APIs over manual reverse-index arithmetic.

