iosswiftxcodedelay

How to create a delay in Swift?


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?

I am using Swift.


Solution

  • 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)
    }
    

    This uses the sleep function from UNIX.