如何跳过for-in循环的迭代(Swift 3)

MyB*_*ome 1 for-in-loop swift swift3

是否有可能跳过Swift 3中for-in循环的迭代?

我想做这样的事情:

for index in 0..<100 {
    if someCondition(index) {
        index = index + 3 //Skip iterations here
    }
}
Run Code Online (Sandbox Code Playgroud)

Raj*_*r R 11

最简单的方法是continue在if条件下使用

       for index in 1...100
       {
            if index == 5
            {
               continue
            }
        print(index)//1 2 3 4 6 7 8 9 10
        }
Run Code Online (Sandbox Code Playgroud)

要么

for index in 1...10 where index%2 == 0
{
  print(index)//2 4 6 8 10
}
Run Code Online (Sandbox Code Playgroud)


Bry*_*hen 5

简单的while循环可以

var index = 0

while (index < 100) {
    if someCondition(index) {
        index += 3 //Skip 3 iterations here
    } else {
        index += 1
        // anything here will not run if someCondition(index) is true
    }
}
Run Code Online (Sandbox Code Playgroud)