ppalancica ppalancica - 27 days ago 16
Swift Question

What's the Swift 3 equivalent of this C for loop?

I am trying to do convert this C code:
int i;

for (i = 9; i >= 0; i--) {

}


in Swift 3, but I am not sure how to do it.

I know that if if I want to do it in ascending order if i, I can simply write:

for i in 0..<10 {

}


But how do I do it in descending order of i?

Thanks in advance!

Answer

Two ways:

// reverse a range
for i in (0...9).reversed() {
    // ...
}

// use stride
for i in stride(from: 9, through: 0, by: -1) {
    // ...
}
Comments