在 NSTask 的源代码中,我在 waitUntilExit 方法中发现了一个有趣的地方:
- (void) waitUntilExit
{
NSTimer *timer = nil;
while ([self isRunning])
{
NSDate *limit = [[NSDate alloc] initWithTimeIntervalSinceNow: 0.1];
if (timer == nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval: 0.1
target: nil
selector: @selector(class)
userInfo: nil
repeats: YES];
}
[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode
beforeDate: limit];
RELEASE(limit);
}
[timer invalidate];
}
我在这里无法理解 NSTimer 的用途。又会调用谁的方法类?
最佳答案
计时器目标是 nil
,所以选择器实际上是无关紧要的:您可以将任何消息发送到 nil
,然后它会被简单地丢弃。
编译器仅验证选择器是否引用了某个已知方法,在本例中为 class
NSObject
协议(protocol)的方法。
此虚拟计时器对于以下 runMode
语句是必需的
否则会立即终止,因为 NSRunLoop
documentation状态:
If no input sources or timers are attached to the run loop, this method exits immediately and returns NO; otherwise.
关于ios - NSTask 源码中有趣的地方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32369870/