0

我正在阅读 NSTask 的标准输出,如下所示:

NSPipe *outputpipe = [[NSPipe alloc] init];
NSFileHandle *output = [outputpipe fileHandleForReading];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedOutputFromAlgo:) name:NSFileHandleReadCompletionNotification object:output];

NSTask* task = [[NSTask alloc] init];
[task setLaunchPath:[NSString stringWithFormat:@"%@algo/Algo", kVinylRoot]];
[task setArguments:[NSArray arrayWithObject:plist]];
[task setStandardOutput:outputpipe];
[task setCurrentDirectoryPath:[NSString stringWithFormat:@"%@algo", kVinylRoot]];

[task launch];
[output readInBackgroundAndNotify];
[task waitUntilExit];

...

-(void)receivedOutputFromAlgo:(NSNotification*)notification {
    [[notification object] readInBackgroundAndNotify];
    NSData* dataOutput = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
    NSString* dataString = [[NSString alloc] initWithData:dataOutput encoding:NSStringEncodingConversionAllowLossy];
    //Do stuff
}

如果我从 Xcode 运行它,这很好。我在控制台中看到了我的任务的标准输出,并且 receivedOutputFromAlgo 被多次击中。但是,如果我存档应用程序并通过双击 .app 包运行它或从终端运行它,它就不起作用。如果我从那里运行它,我仍然可以在 console.app 或终端中看到任务标准输出。

在这一点上它不被认为是标准输出吗?为什么这行不通?

编辑:我只是尝试在发布中关闭优化,但也没有用。

4

1 回答 1

0

您需要处理 setStandardError,如下所示:

[task setStandardError:outputpipe];
于 2014-09-22T02:37:26.813 回答