我有这个,但看起来太复杂了:
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()
}
最佳答案
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue()) {
self.callFinished()
}
这是很棒的多功能库的一部分 Grand Central Dispatch ,这将使您不仅可以执行延迟,还可以执行各种并发操作,您几乎肯定会遇到线程错误!幸运的是,由于在您的情况下您正在回调主队列,因此您的代码都不会并发执行,因此在这种情况下无需担心。
关于ios - 在 Swift 中,在 5 秒内(非阻塞)调用一个方法最简单的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29616310/