首先,当在Xcode中调试和运行时,一切都会按预期进行。

但是,当我尝试“共享”我的应用程序(即发布版本)时,我的NSTask在输出standardErrors时不会输出任何standardOutput。那怎么可能?

我的密码

- (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];
    }

最佳答案

/usr/bin/compass不是OSX的标准安装中安装的二进制文件(我的Mac上compass中没有名为/usr/bin的二进制文件)

因此,当您的应用程序在未安装/usr/bin/compass的另一台Mac上运行时,并且您尝试运行此任务时,它找不到并仅在stderr上输出一些错误,这似乎很合逻辑。

关于objective-c - NSTask在发行版本中仅返回standardError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8254680/

10-10 17:05