Swift newbie question

Hi Guys,


Can any one help me understand why this code:


for counter in stride(from: 10, to:0, by: -1){

print(counter)

}


counts 10,9,8...1 not 10,9,8...0


Somehow changing it to


for counter in stride(from: 10, to:-1, by: -1){

print(counter)

}


does not feel right.


Regards Gethyn

Accepted Reply

Use stride(from:through:) instead of stride(from:to:).

Replies

Use stride(from:through:) instead of stride(from:to:).

Or this, which is a lot clearer IMO.

for counter in (0...10).reversed() {
    print(counter) 
}

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"