dispatch_after - GCD in Swift?

前端 未结 25 2448
心在旅途
心在旅途 2020-11-21 06:07

I\'ve gone through the iBook from Apple, and couldn\'t find any definition of it:

Can someone explain the structure of dispatch_after?

d         


        
25条回答
  •  野性不改
    2020-11-21 06:44

    Swift 3.0 version

    Following closure function execute some task after delay on main thread.

    func performAfterDelay(delay : Double, onCompletion: @escaping() -> Void){
    
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + delay, execute: {
           onCompletion()
        })
    }
    

    Call this function like:

    performAfterDelay(delay: 4.0) {
      print("test")
    }
    

提交回复
热议问题