Stack108 Stack108 - 6 months ago 21
Swift Question

swift 2.2 new syntax for loops

i have to modify my for loops of my swift 2 app.
at the moment i use this syntax

for (var x = 0; x < 5; x++) {


i learned that i have to use this:

for x in 0..<5 {


but how i have to change this for loop:

for (var x = 0; x < 6; x = x+2) {

Answer

use stride function

 // for x<6
 for i in 0.stride(to: 6, by: 2) {
    print(i)   // 0,2,4
 }

 //for x<=6
 for i in 0.stride(through: 6, by: 2) {
    print(i)  // 0,2,4,6
 }