Add delay in Swift

Adding delay is bad. But sometimes for philosophical, legal and professional reasons we do desire to add delays in our code.

Below is the code for method to add a delay in the Swift (< 3.0).


function addDelay(with desiredDelayInSeconds: Double) {
    let delay = desiredDelayInSeconds * Double(NSEC_PER_SEC)
    let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
     dispatch_after(time, dispatch_get_main_queue()) {
         <Do some processing you wish to do after specified delay>
    }
}

Jayesh Kawli

I am a web and mobile developer working at Wayfair in Boston, MA. I come to learn so many things during course of life and I write about things which helped me and feel like they can help others too.

Subscribe to Fresh Beginning

Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!