First of all, protocol extensions change what is reverse used:
for i in (1...5).reverse() { print(i) } // 5 4 3 2 1
Stride have been reworked in Xcode 7 Beta 6. The new usage is:
ForIInch 0.stride(To: -8, By: -2) { Print(I } //0-2 -4-6for I in 0. Stride (through: -< Span class= "lit" >8, by: -2) { print (i } //0-2 -4-6-8
It also works for Doubles :
for i in 0.5.stride(to:-0.1, by: -0.1) { print(i) }
Be wary of floating point compares here for the bounds.
Earlier Edit for Swift 1.2: As of Xcode 6 Beta 4, by and Reverserange don ' t exist anymore: [
If you were just looking to reverse a range, the reverse function was all you need:
for i in reverse(1...5) { println(i) } // prints 5,4,3,2,1
As posted by 0x7fffffff There are a new stride construct which can be used to iterate and increment by AR Bitrary integers. Apple also stated, floating point, is coming.
Sourced from his answer:
ForXInchStride(From: 0,Through: -8, By: -2) {println(X) 0,-2,-4,-6,-8}for x in Stride(from: 6, to: -2, by: -4) { println(x) //6, 2}
Reverse Loop in Swift