title: create delay in swift 3 inside a loop
body: <p>I know I can delay a part of code in Swift 3 using the followign syntax (<a href="https://stackoverflow.com/questions/38031137/how-to-program-a-delay-in-swift-3">How to program a delay in Swift 3</a>):</p>
  
  <pre><code>let when = DispatchTime.now() + 2 // change 2 to desired second delay.
  DispatchQueue.main.after(when: when) {
  // run your code with delay
  }
  </code></pre>
  
  <p>Or a similar post: <a href="https://stackoverflow.com/questions/37805885/how-to-create-dispatch-queue-in-swift-3">How to create dispatch queue in Swift 3</a></p>
  
  <p>However these are not the delay methods I could use. I need to insert a delay in the loop. For example imagine that there's a label component and a button. When I click the button, I want the label to show the loop variable consecutively:</p>
  
  <pre><code>@IBOutlet weak var showIntegers: UILabel!
  
  @IBAction func countNums(_ sender: AnyObject) {
  
      for i in 1...5 {
          showIntegers.text = String(i)
          //delay code here, sleep(1) doesn't work
      }
  }
  </code></pre>
  
  <p>I used sleep as the delay but then the app sleeps for 5 seconds and then displays 5. I cannot see 1, 2, 3, 4 and 5 consecutively with 1 seconds of delay.</p>
  
  <p>I also couldn't figure out how I can use the DispatchQueue class inside the loop. Thanks for your help in advance.</p>