0
嘗試讀取從NSTask返回的數據會導致永不返回的掛起。我已經驗證我的腳本正在運行,實際上是將數據返回給stdout和stderr。這是一個簡單的兩行shell腳本,它將一行發送到標準輸出,另一行發送到標準錯誤。NSTask掛在readDataToEndOfFile
的輸出的NSLog說
了手柄:< NSConcreteFileHandle:0x10010a800 >
然後它只是掛起。這是我正在使用的代碼。
NSPipe *stderr = [NSPipe pipe];
NSPipe *stdout = [NSPipe pipe];
NSTask *task = [[NSTask alloc] init];
task.standardError = stderr;
task.standardOutput = stdout;
task.standardInput = [NSPipe pipe];
task.launchPath = @"/tmp/f";
[task launchPath];
[task waitUntilExit];
NSFileHandle *fh = [stderr fileHandleForReading];
NSLog(@"Got handle: %@", fh);
[fh readDataToEndOfFile];
NSLog(@"Read it");
OMG,這是尷尬的! – Gargoyle