3

This is basically code asking.

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)
     } 

}

I had try, if the statement true, it's still try to complete loop on otherObjList. By the way, I wanna break this when statement true and continue loop on for objList.

14
  • Use break when you found object. @rmaddy i think it is break Commented Feb 23, 2018 at 5:04
  • @rmaddy - Don't you mean break? Commented Feb 23, 2018 at 5:04
  • Yes I wanna break that statement and continue loop objList. @TedHopp Commented Feb 23, 2018 at 5:05
  • @TedHopp break will exit the loop Commented Feb 23, 2018 at 5:05
  • @rmaddy sorry if i am wrong but; it's still try to complete loop on otherObjList. By the way, I wanna break this when statement true and continue loop on for objList. Commented Feb 23, 2018 at 5:07

2 Answers 2

6

You seem to be looking for continue.

Here's a simple example of the difference between continue and 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()

In short break leaves the surrounding loop immediately, whilst continue aborts the current iteration and continues the loop with the next iteration.

Sign up to request clarification or add additional context in comments.

1 Comment

to make sure that my statement in "loop for" is not normal as your explanation.
5

It sounds like you just want this:

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.
}

1 Comment

It works in my test. If you want more help, you need to post the details of the error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.