如何在Swift中创建延迟? [英] How to create a delay in Swift?

查看:124
本文介绍了如何在Swift中创建延迟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想暂时暂停我的应用。换句话说,我希望我的应用执行代码,但是在某个时候暂停4秒钟,然后继续执行其余的代码。我该怎么做?

I want to pause my app at a certain in point. In other words, I want my app to execute the code, but then at a certain point, pause for 4 seconds, and then continue on with the rest of the code. How can I do this?

我正在使用Swift。

I am using Swift.

推荐答案

考虑使用 NSTimer 或调度计时器,而不是睡眠(如果会从UI线程调用会锁定您的程序)。

Instead of a sleep, which will lock up your program if called from the UI thread, consider using NSTimer or a dispatch timer.

但是,如果您确实需要延迟当前线程,则:

But, if you really need a delay in the current thread:

do {
    sleep(4)
}

这使用 sleep 函数UNIX。

This uses the sleep function from UNIX.

这篇关于如何在Swift中创建延迟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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