[NOTE this is my old page saved for enjoyment of future generations, many links dead]

Jeff McLellan | Data Analysis & App Developer

Swift 2 ranges

There was been some changes to Ranges in Swift. The code below works in Swift 2.0

for i in (1...5).reverse() {
    print(i)
} // 5 4 3 2 1

for i in (5..<10) {
    print(i)
} // 5 6 7 8 9

for i in 0.stride(to: -10, by: -2) {
    print(i)
} // 0 -2 -4 -6 -8

for i in 0.stride(through: -10, by: -2) {
    print(i)
} // 0 -2 -4 -6 -8 -10

for i in 0.3.stride(to:0.9, by: 0.3) {
    print(i)
} // 0.3 0.6 0.9