【问题标题】:Cannot read stdout from an NSTask in release mode在发布模式下无法从 NSTask 读取标准输出
【发布时间】:2023-03-20 22:28:01
【问题描述】:

我正在像这样读取 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 或终端中看到任务标准输出。

此时它不被视为标准输出吗?为什么这不起作用?

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

【问题讨论】:

    标签: objective-c macos stdout nstask nspipe


    【解决方案1】:

    你需要处理 setStandardError,像这样:

    [task setStandardError:outputpipe];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-29
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多