快速延迟功能 [英] Delaying function in swift

查看:131
本文介绍了快速延迟功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有代码样本或任何东西,因为我不知道该怎么做,但是有人能告诉我如何在一段时间内用swift延迟函数吗?

$您可以使用GCD(例如延迟10秒):

Swift 2:

  let triggerTime =(Int64(NSEC_PER_SEC)* 10)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,triggerTime) ,dispatch_get_main_queue(),{() - >无效
self.functionToCall()
})

Swift 3:

  DispatchQueue.main.asyncAfter(截止日期:.now()+ 10.0,执行:{
self.functionToCall()
})


I don't have a code to sample or anything, because I have no idea how to do it, but can someone please tell me how to delay a function with swift for a set amount of time?

解决方案

You can use GCD (in the example with a 10 second delay):

Swift 2:

let triggerTime = (Int64(NSEC_PER_SEC) * 10)
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, triggerTime), dispatch_get_main_queue(), { () -> Void in
    self.functionToCall()
})

Swift 3:

DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
    self.functionToCall()
})

这篇关于快速延迟功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆