if语句为真的Swift Break循环

Lea*_*eat 2 swift

这基本上是代码询问。

for obj in objList {

    if otherObjList.contains(where: { $0.localString == obj.localString}) {
       //if this statement true, I wanna break this statement and 
       //continue loop on the above list (objList)
     } 

}
Run Code Online (Sandbox Code Playgroud)

我试过,如果语句为真,它仍然尝试在 otherObjList 上完成循环。顺便说一句,我想在语句为 true 时打破这个,并继续为 objList 循环。

Gri*_*mxn 6

你似乎正在寻找continue

continue下面是一个简单的例子来说明和 之间的区别break

// break
// Prints 1,2,3,4,5
for i in 1 ... 10 {
    print(i, terminator: "")
    if i == 5 {
        break
    }
    print(",", terminator: "")
}
print()

// continue
// Prints 1,2,3,4,56,7,8,9,10,
for i in 1 ... 10 {
    print(i, terminator: "")
    if i == 5 {
        continue
    }
    print(",", terminator: "")
}
print()
Run Code Online (Sandbox Code Playgroud)

简而言之,break立即离开周围的循环,同时continue中止当前迭代并继续下一次迭代的循环。


rob*_*off 5

听起来你只想要这个:

for obj in objList {

    if otherObjList.contains(where: { $0.localString == obj.localString }) {
        continue
    }

    // Statements here will not be executed for elements of objList
    // that made the above if-condition true. Instead, the for loop
    // will execute from the top with the next element of objList.
}
Run Code Online (Sandbox Code Playgroud)