阻止从 NSTask 运行的程序中输出标准



我正在使用带有setStandardOutput的NSTask将命令行工具的标准输出重定向到我的程序,因此我可以使用它进行处理。这很好用,但作为副作用,当我运行程序时,该工具的输出显示为输出。我想完全重定向它,以便它只转到我指定的 NSPipe 对象。

这可能吗?下面的示例显示了输出标准输出,但我也想在标准误差下执行此操作。

NSTask *ls=[[NSTask alloc] init];
NSPipe *pipe=[[NSPipe alloc] init];
NSFileHandle *handle;
NSString *string;
[ls setLaunchPath:@"/bin/ls"];
[ls setArguments:[NSArray arrayWithObjects:@"-l",@"/System",nil]];
[ls setStandardOutput:pipe];
handle=[pipe fileHandleForReading];
[ls launch];
string=[[NSString alloc] initWithData:[handle readDataToEndOfFile]
    encoding:NSASCIIStringEncoding]; 

我做了更多的测试,意识到上面的代码做了我想要的,并且当我的程序运行时不显示输出,所以看起来我毕竟没有问题。(:

最新更新