问题描述
首先,当在Xcode中调试和运行时,一切正常工作。
First of all, when debugging and running in Xcode everything works as expected.
但是当我试图共享我的应用程序,我的NSTask将不会输出任何standardOutput,而standardErrors推出。如何实现?
But when I try to "share" my app, i.e. make a release build, my NSTask won't output any standardOutput while standardErrors ARE put out. How is that possible?
我的代码
- (id)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:window];
if (self) {
// Initialization code here.
}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readPipe:) name:NSFileHandleReadCompletionNotification object:nil];
return self;
}
-(void) watchFile:(NSNotification *)notification {
NSString *path = [[notification userInfo] valueForKey:@"path"];
task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/compass"];
[task setCurrentDirectoryPath:path];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"watch",@"--boring", nil];
[task setArguments: arguments];
NSPipe *outPipe, *errPipe;
outPipe = [NSPipe pipe];
errPipe = [NSPipe pipe];
[task setStandardOutput: outPipe];
[task setStandardError: errPipe];
[task setStandardInput: [NSPipe pipe]];
standardHandle = [outPipe fileHandleForReading];
[standardHandle readInBackgroundAndNotify];
errorHandle = [errPipe fileHandleForReading];
[errorHandle readInBackgroundAndNotify];
[self setSplitterPosition:0.0f];
[task launch];
}
-(void)readPipe:(NSNotification *)notification {
NSLog(@"reading pipe");
NSData *data;
NSString *text;
if(!([notification object] == standardHandle) && !([notification object] == errorHandle)) {
return;
}
data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem];
text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
if ([data length] == 0) {
//error
[self setSplitterPosition:150.0f];
return;
}
[terminalViewController updateTerminal:text];
if(![text isEqualToString:@"\n"]) [self growlAlert:text title:@"Compapp"];
[text release];
if(task) [[notification object] readInBackgroundAndNotify];
}
推荐答案
问题,然后检查我在此链接张贴的答案:
if this is still an open issue for you, then checkout the answer I posted at this link: How to use a determinate NSProgressIndicator to check on the progress of NSTask? - Cocoa
它提供了一个用于创建异步NSTask和从标准输出或标准错误中读取的好模板。
it provides a good template for creating an async NSTask and for reading from standard output or standard error.
这篇关于NSTask只在发布版本中返回standardError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!