Mac OS,控制台应用程序. performSelector:withObject:afterDelay:不起作用吗? [英] Mac OS, console application. performSelector:withObject:afterDelay: doesn't work?
问题描述
我在其中创建了一个简单的单例并运行方法:
I created a simple singleton and run method in it:
- (void)run {
static int times = 0;
NSLog(@"times = %d", times++);
[self performSelector:@selector(run) withObject:nil afterDelay:MIN_DELAY];
}
但是它不能正常工作.它只能执行一次.
But it doesn't work properly. It is executed only once.
但是,如果我将performSelector:withObject:afterDelay:
替换为performSelector:
,那么它将被调用很多次(但我需要在两次调用之间延迟).
But if I replace performSelector:withObject:afterDelay:
with performSelector:
then it will be called a lot of times (but I need a delay between calls).
那么为什么方法performSelector:withObject:afterDelay:
不起作用?我可以完全使用这种方法吗?
So why method performSelector:withObject:afterDelay:
doesn't work? And can I use this method at all?
推荐答案
来自文档:
此方法向其当前上下文的运行循环进行注册,并依赖于定期运行的运行循环才能正确执行.
This method registers with the runloop of its current context, and depends on that runloop being run on a regular basis to perform correctly.
您没有运行循环.实际上,此方法无法为您正确执行.
You have no runloop. Ipso facto, this method does not perform correctly for you.
(创建和启动运行循环是调用UIApplicationMain
的工作之一,但是当然您永远不会调用它.)
(Creating and starting a runloop is one of the things that calling UIApplicationMain
does, but of course you are never calling it.)
这篇关于Mac OS,控制台应用程序. performSelector:withObject:afterDelay:不起作用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!