1

I have this, but seems overly complicated:

    var connectionTimer = MZTimerLabel(timerType: MZTimerLabelTypeTimer)
    connectionTimer.delegate = self
    connectionTimer.tag = 0
    connectionTimer.setCountDownTime(5)
    connectionTimer.start()


    func timerLabel(timerLabel: MZTimerLabel!, finshedCountDownTimerWithTime countTime: NSTimeInterval) {
          self.callFinished()
    }
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    See my answer here: http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861 – matt Apr 13 '15 at 23:01

2 Answers2

5
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue()) {
    self.callFinished()
}

This is part of the awesome and versatile library Grand Central Dispatch, which will allow you to perform not just delays but all sorts of concurrent operations you will almost certainly have threading bugs in! Luckily, since in your case you are calling back to the main queue, none of your code will execute concurrently, so there's nothing to worry about in this case.

andyvn22
  • 14,696
  • 1
  • 52
  • 74
  • Can you elaborate on threading bugs? Can I just stick this in my code, or should I be worried about threading bugs? – TIMEX Apr 13 '15 at 23:07
  • @TIMEX Edited; that did sound scarier than I intended. :) – andyvn22 Apr 13 '15 at 23:09
  • I'm not referring to the inner line, didn't expect to spell that out – Stephen J Apr 14 '15 at 13:27
  • @StephenJ According to [the documentation](https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html) this exists in Swift too. (You may need to click `Swift` in the top-right corner.) – 11684 May 05 '15 at 13:28
-1

Try this

let delayTimer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: Selector("delayed"), userInfo: nil, repeats: false)

func delay()  { println ("hi") }

It will call delay five seconds later.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
qwerty_so
  • 35,448
  • 8
  • 62
  • 86