iOS_devloper iOS_devloper - 8 days ago 7
iOS Question

Swift range operator for i = mymax; i >= 0 i--

I facing problem in executing for loop in Swift 3. I can use loop for range operator

...
and
..<
but in may case, I want something like
..>
but its not available.

How do I execute following loop in Swift 3?

var myMax = 20
for var i = myMax ; i >= 0 ; i -= 1 {
...
}

Answer

It's easy to reverse the loop. User reversed function.

Swift 3

let myMax = 20
for i in (1..<myMax).reversed() {
    print(i)
}

You can also use stride as @ZaidPathan said :

This question have all answers with all versions : How to iterate for loop in reverse order in swift?

Comments