how to call a method of multiple arguments with delay

后端 未结 6 575
谎友^
谎友^ 2020-12-13 04:15

I\'m trying to call a method after some delay.

I know there is a solution for that:

[self performSelector:@selector(myMethod) withObject:nil afterDel         


        
6条回答
  •  感情败类
    2020-12-13 04:49

    Here is how you can trigger a block after a delay in Swift:

    runThisAfterDelay(seconds: 5) { () -> () in
        print("Prints this 5 seconds later in main queue")
        //Or perform your selector here
    }
    
    /// EZSwiftExtensions
    func runThisAfterDelay(seconds seconds: Double, after: () -> ()) {
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(seconds * Double(NSEC_PER_SEC)))
        dispatch_after(time, dispatch_get_main_queue(), after)
    }
    

    The argument count does not really matter.

    Its included as a standard function in my repo: https://github.com/goktugyil/EZSwiftExtensions

提交回复
热议问题