本文介绍了您如何在延迟后触发块,例如 -performSelector:withObject:afterDelay:?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在延迟后调用带有原始参数的块,例如使用 performSelector:withObject:afterDelay: 但带有像 int/double/float?

Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float?

推荐答案

我想你正在寻找 dispatch_after().它要求您的块不接受任何参数,但您可以让块从本地范围捕获这些变量.

I think you're looking for dispatch_after(). It requires your block to accept no parameters, but you can just let the block capture those variables from your local scope instead.

int parameter1 = 12;
float parameter2 = 144.1;

// Delay execution of my block for 10 seconds.
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 10 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
    NSLog(@"parameter1: %d parameter2: %f", parameter1, parameter2);
});

更多:https://developer.apple.com/documentation/dispatch/1452876-dispatch_after

这篇关于您如何在延迟后触发块,例如 -performSelector:withObject:afterDelay:?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 11:06