简体   繁体   中英

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

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];
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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