NSNotification延迟

pkc*_*456 2 nsnotificationcenter ios swift ios9

如何NSNotification延迟发布.

我想到的一个解决方案: - 在performSelectorAfterDelay方法中发布通知.对此有没有更好的解决方案.

NSNotificationQueue可以帮助我实现这一目标?

ZeM*_*oon 11

利用GCD的dispatch_after()方法.在Objective-C中,这看起来像:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    [[NSNotificationCenter defaultCenter] postNotification:someNotification];
});
Run Code Online (Sandbox Code Playgroud)

更新:Swift 3

DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(5)) { 
    NotificationCenter.default.post(someNotification)
}
Run Code Online (Sandbox Code Playgroud)