在Swift中,在5秒内调用方法的最简单方法是什么(非阻塞)?

TIM*_*MEX 1 ios swift

我有这个,但似乎过于复杂:

    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()
    }
Run Code Online (Sandbox Code Playgroud)

and*_*n22 5

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue()) {
    self.callFinished()
}
Run Code Online (Sandbox Code Playgroud)

这是令人敬畏的多功能图书馆Grand Central Dispatch的一部分,这将使您不仅可以执行延迟,而且可以执行各种并发操作,您几乎肯定会遇到漏洞!幸运的是,因为在你的情况下你正在回调主队列,你的代码都不会同时执行,因此在这种情况下没有什么可担心的.