Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to know when a loop has ended?

Please, forgive me as i'm very new to swift, and programming in general.

Believe me when I say I've tried hard to understand this, but I simply can't and would greatly appreciate any help.

say I have this function:

func loop() {
  for var i=0; i<5; i++ {
  println(i)
  }
}

and I wanted to print to the logs "loop has finished" once this loop had completed and finished running, how would I do this? If i do:

func loop() {
  for var i=0; i<5; i++ {
  println(i)
  println("loop has finished")
  }
}

then "loop has finished" gets printed after every time i is incremented.

I've tried reading into closures and completion handlers, but it's all going over my head at the moment and I don't really understand how i'd achieve the task above.

If there's an angel that can show me how I'd complete my above example, I'd be in your debt.

Thankyou for your patience with my currently pathetic knowledge!

like image 948
dan martin Avatar asked Aug 03 '15 01:08

dan martin


People also ask

How do you find the end of a loop?

Just separate the last thing from the loop. Note the use of str. length - 1 in the condition. In a forEach loop, you must iterate linearly over the array, so some conditional logic and a counter are necessary to detect the last element.

How do you end a loop in Swift?

You can exit a loop at any time using the break keyword. To try this out, let's start with a regular while loop that counts down for a rocket launch: var countDown = 10 while countDown >= 0 { print(countDown) countDown -= 1 } print("Blast off!")

What is the end of for loop?

break terminates the execution of a for or while loop. Statements in the loop after the break statement do not execute. In nested loops, break exits only from the loop in which it occurs. Control passes to the statement that follows the end of that loop.

What is a loop condition check?

The condition is tested at the beginning of each iteration of the loop. If the condition is true ( non-zero ), then the body of the loop is executed next. If the condition is false ( zero ), then the body is not executed, and execution continues with the code following the loop.


2 Answers

To produce the same result as what others have posted but with basic closure syntax:

func printFunction() {
    println("loop has finished")
}

func loopWithCompletion(closure: () -> ()) {
    for var i=0; i<5; i++ {
        println(i)
    }
    closure()
}

This is how you call it:

 loopWithCompletion(printFunction)

Swift 3 Update:

func printFunction() {
    print("loop has finished")
}

// New for loop syntax and naming convention
func loop(withCompletion completion: () -> Void ) {
    for i in 0 ..< 5 {
        print(i)
    }
    completion()
}

Then call it like this:

loop(withCompletion: printFunction)

Or

loop {
    print("this does the same thing")
}
like image 74
johnslay Avatar answered Sep 17 '22 18:09

johnslay


The reason why your code doesn't work is because you have the println() statement inside the loop, like this:

func loop() {
  for var i=0; i<5; i++ {
  println(i)
  println("loop has finished")
  }
}

So it prints out "Finished" every time the loop loops.

To fix this, all you need to do is put the println() statement after the loop, like so:

func loop() {
  for var i=0; i<5; i++ {
    println(i)
  }
  println("loop has finished")
}

And voilà. Your app will now function properly!

like image 34
CraftMcMatt Avatar answered Sep 17 '22 18:09

CraftMcMatt