简体   繁体   English

延迟后可以执行类方法吗?

[英]Can I perform a class method after a delay?

I'm inside a class method, and I want to trigger another class method, from the same class, after a delay. 我在一个类方法中,我想在延迟后从同一类触发另一个类方法。 Doesn't appear that I can use 看来我可以使用

[MyClass performSelector:@selector(myMethod) withObject:nil afterDelay:1]

Any other options? 还有其他选择吗?

Edited: 编辑:

My bad. 我的错。 I assumed that call wasn't available within a class method, because it wasn't auto-completing as I wrote it. 我以为该调用在类方法中不可用,因为它在我编写时没有自动完成。 As a couple of people have pointed out here, it actually works fine. 正如一些人在这里指出的那样,它实际上可以正常工作。 Not sure why it didn't autocomplete in my case, but learned a lesson about making assumptions when this happens. 不知道为什么在我的案例中它不会自动完成,但是在发生这种情况时吸取了关于进行假设的教训。 Thanks everyone. 感谢大家。

您可以使用-class获得对类的实际引用,然后像下面这样发送消息:

[[MyClass class] performSelector:@selector(myClassMethod) withObject:nil afterDelay:1.0];

You can also use dispatch_after 您也可以使用dispatch_after

double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
    [MyClass myClassMethod];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM