Add delay in Swift

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