Swift - How to know when a loop has ended?

前端 未结 5 1728
我寻月下人不归
我寻月下人不归 2020-12-30 11:52

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 ca

5条回答
  •  孤城傲影
    2020-12-30 12:47

    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!

提交回复
热议问题