Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocoa 如何使用NSNotification检查NSTask的状态_Cocoa - Fatal编程技术网

Cocoa 如何使用NSNotification检查NSTask的状态

Cocoa 如何使用NSNotification检查NSTask的状态,cocoa,Cocoa,如何使用NSNotification检查NSTask的状态?我知道NSTask中有几个类方法,但我并不真正理解如何在Cocoa应用程序中实现它们。有人能帮我吗?这篇博客文章介绍了如何使用NSNotification从NSTask接收通知 如果您知道您的任务不是长期运行的,那么有一个更简单的解决方案。我对另一个SO问题的回答有一个使用NSTask的示例,其中包括返回任务的状态,而不返回NSNotification谢谢Gordon,我已经尝试了您给出的解决方案,但让我困惑的是当我编写代码时: NSP

如何使用NSNotification检查NSTask的状态?我知道NSTask中有几个类方法,但我并不真正理解如何在Cocoa应用程序中实现它们。有人能帮我吗?

这篇博客文章介绍了如何使用
NSNotification
NSTask
接收通知


如果您知道您的任务不是长期运行的,那么有一个更简单的解决方案。我对另一个SO问题的回答有一个使用
NSTask
的示例,其中包括返回任务的状态,而不返回
NSNotification
谢谢Gordon,我已经尝试了您给出的解决方案,但让我困惑的是当我编写代码时:

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

任务可以返回什么样的输出数据?在我的情况下,控制台窗口会被卡住,没有返回的内容会被卡住,如果您不读取它在stdout/stderr上发送的内容,任务就会被卡住。这就是为什么需要创建管道并使用
setStandardOutput
对其进行设置

使用
[nsfilehandlereadtoendofileinbackgroundandnotify]
并等待
nsfilehandlereadtoendofilecompletionnotification
。它将为您提供回调中的所有数据