我为NSFileHandleReadCompletionNotification
.
我用两个独立的管道设置了标准 I/O。
NSPipe * input = NSPipe.new;
NSPipe * output = NSPipe.new;
[serverTask setStandardInput:input];
[serverTask setStandardOutput:output];
我启动一个执行 Java jar 的 NSTask,并开始读取数据。
[[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];
我不断读取数据并将数据附加到一个NSTextView
新数据中:
- (void)serverLogHasChanged:(NSNotification *)notification
{
[[serverTask.standardOutput fileHandleForReading] readInBackgroundAndNotify];
NSData * newData = [notification.userInfo objectForKey:NSFileHandleNotificationDataItem];
if (newData != nil && availableData != newData)
{
NSMutableString * serverLogString = [NSMutableString stringWithString:serverLog.string];
[serverLogString appendString:[NSString.alloc initWithData:newData encoding:NSUTF8StringEncoding]];
[serverLog setString:serverLogString];
[serverLog.enclosingScrollView.contentView scrollPoint:NSMakePoint(0, NSMaxY(serverLog.enclosingScrollView.contentView.bounds))];
}
newData = availableData;
}
但是,我得到了奇怪的输出到NSTextView
那些“>”字符应该是实际输出的行,但输出最终会出现在 Xcode 的控制台中。
换句话说,控制台打印输出而不是我NSPipe
的,它只打印输出新行的指示。