Delaying function in swift

<>

This question already has an answer here:

Answers


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 and Swift 4
DispatchQueue.main.asyncAfter(deadline: .now() + 10.0, execute: {
    self.functionToCall()
})

Swift 3 Version for a delay of 10 seconds

unowned let unownedSelf = self

let deadlineTime = DispatchTime.now() + .seconds(10)
DispatchQueue.main.asyncAfter(deadline: deadlineTime, execute: { 
     unownedSelf.functionToCall()
})

 NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "functionHere", userInfo: nil, repeats: false)

This would call the function functionHere() with a 3 seconds delay


For adding argument to delay function.

First setup a dictionary then add it as the userInfo. Unwrap the info with the timer as the argument.

let arg : Int = 42
let infoDict : [String : AnyObject] = ["argumentInt", arg]

NSTimer.scheduledTimerWithTimeInterval(NSTimeInterval(3), target: self, selector: "functionHereWithArgument:", userInfo: infoDict, repeats: false)

Then in the called function

func functionHereWithArgument (timer : NSTimer)
{
    if let userInfo = timer.userInfo as? Dictionary<String, AnyObject>
    {
         let argumentInt : Int = (userInfo[argumentInt] as! Int)
    }
}

Need Your Help

java.lang.UnsatisfiedLinkError: Couldn't load stlport_shared: findLibrary returned null (tess-two)

java android android-ndk tesseract sqlcipher

I am using sqlcipher.jar for encrypting database in android and also using it's native library in

Ignore header line when parsing CSV file

ruby-on-rails-3 csv

How can the header line of the CSV file be ignored in ruby on rails while doing the CSV parsing!! Any ideas