objective-c - NSTask 在发布版本中仅返回 standardError

标签 objective-c macos cocoa nstask nsfilehandle

首先,在 Xcode 中调试和运行时,一切都按预期工作。

但是当我尝试“共享”我的应用程序时,即进行发布构建时,我的 NSTask 将不会输出任何标准输出,而标准错误会被输出。这怎么可能?

我的代码

- (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 标准安装中安装的二进制文件(我的 /中没有任何名为 compass 的二进制文件usr/bin 在我的 Mac 上)

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

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

相关文章:

objective-c - 等待 NSStream 响应时替换对 sleep 的调用

objective-c - 所有 NSTableView 标题都显示排序顺序箭头?

objective-c - 图片幻灯片放映

objective-c - NSTableView 不显示 NSMutableArray 的内容

c# - SSCrypto/OpenSSL 到 C# 加密

objective-c - 从 Cocoa IBAction 启动 WebKit Inspector?

objective-c - 添加/删除由 NSArrayController 管理的 NSMutableArray 的最佳方法

iphone - 复选标记单选

ios - 我可以以编程方式滚动到 UIPickerView 中所需的行吗?

ruby-on-rails - 一键安装 Ruby/Rails/SQLite?