Use for var i in 0..<10 { to overcome the error.
i in for i in 1..<10 is, in fact, an override of i in the for scope, which by default is let and overrides your previous declaration. Do not imagine what your logic is doing, the mind, increasing i in the middle of the cycle. This will not affect the number of cycles performed by the cycle - see below:
var i: Int = -1 print("Outer scope, i=\(i)") // i=-1 for var i in 0..<10 { // Will be executed 10 times, regardless of what you do to i in the loop print("Inner scope, i=\(i)") // i=0...9, including all if i == 2 { i = i + 10 print("Inner, modified i=\(i)") // i=12 } } print("Outer scope, i=\(i)") // i=-1 /* Complete output: Outer scope, i=-1 Inner scope, i=0 Inner scope, i=1 Inner scope, i=2 Inner, modified i=12 Inner scope, i=3 Inner scope, i=4 Inner scope, i=5 Inner scope, i=6 Inner scope, i=7 Inner scope, i=8 Inner scope, i=9 Outer scope, i=-1 */
The important point is that the Swift for i in loop is not a C for (i=0; i<10; i++) loop for (i=0; i<10; i++) .
source share