0
for (int i = n-2; i >= 0; --i)
{
....
}

the automatic translation of the above to swift 3 syntax is this

for i in n-2 ... 0 
{
}

this doesn't work, because n could be 1, in the c syntax, this is valid, the loop won't be triggered,

but in the swift 3 syntax, this will cause runtime error.

Bill Yan
  • 3,369
  • 4
  • 27
  • 42

2 Answers2

2
for i in stride(from: n-2, through: 0, by: -1) {

}
Alexander
  • 59,041
  • 12
  • 98
  • 151
1
for i in (0 ... n-2).reversed() {

}
nathangitter
  • 9,607
  • 3
  • 33
  • 42