ios - NSTask 源码中有趣的地方

标签 ios objective-c macos cocoa

在 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/

相关文章:

html - UIWebView中的HTML文件滚动非常慢-Pdf2htmlEX

ios - 使两个按钮对齐到两个边缘的 VFL

iOS PWA 抑制工具栏

objective-c - 在每次打开时修改 NSMenu 结构?

C HelloWorld exec 格式错误

java - 如何在 Mac os x 中将 Java 应用程序与本地数据库连接

bash - 来自 bash 脚本的 `security unlock-keychain`

ios - 什么时候在 iOS 中检索图像高度是安全的

ios - GStreamer 中架构 arm64 的 undefined symbol

objective-c - 无法在 SWIFT 中解包 NSAppleEventDescriptor