dispatch_after - GCD in Swift?

前端 未结 25 2387
心在旅途
心在旅途 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:27

    Swift 3 & 4:

    You can create a extension on DispatchQueue and add function delay which uses DispatchQueue asyncAfter function internally

    extension DispatchQueue {
        static func delay(_ delay: DispatchTimeInterval, closure: @escaping () -> ()) {
            let timeInterval = DispatchTime.now() + delay
            DispatchQueue.main.asyncAfter(deadline: timeInterval, execute: closure)
        }
    }
    

    use:

    DispatchQueue.delay(.seconds(1)) {
        print("This is after delay")
    }
    

提交回复
热议问题