dispatch_after - GCD in Swift?

前端 未结 25 2487
心在旅途
心在旅途 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条回答
  •  猫巷女王i
    2020-11-21 06:33

    This worked for me.

    Swift 3:

    let time1 = 8.23
    let time2 = 3.42
    
    // Delay 2 seconds
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
        print("Sum of times: \(time1 + time2)")
    }
    

    Objective-C:

    CGFloat time1 = 3.49;
    CGFloat time2 = 8.13;
    
    // Delay 2 seconds
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGFloat newTime = time1 + time2;
        NSLog(@"New time: %f", newTime);
    });
    

提交回复
热议问题