0
다음 Obj-C 코드와 로그 출력이 있습니다. 누구나 NSFileHandle의 출력이 왜 나오지 않는지 말할 수 있습니까?NSTask 및 readInBackgroundAndNotify 에코가 작동하지 않음
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self performSelectorInBackground:@selector(startTask:) withObject:nil];
}
- (void) startTask: (id) sender
{
NSPipe *pipe = [[NSPipe alloc] init];
NSFileHandle *fh = pipe.fileHandleForReading;
[fh readInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(output:) name:NSFileHandleReadCompletionNotification object:fh];
NSTask *echoTask = [[NSTask alloc] init];
echoTask.standardOutput = pipe;
echoTask.standardError = [[NSPipe alloc] init];
echoTask.launchPath = @"/bin/echo";
echoTask.arguments = @[@"hello world!"];
NSLog(@"launching...");
[echoTask launch];
[echoTask waitUntilExit];
NSLog(@"finished.");
}
- (void) output:(NSNotification *)notification
{
NSFileHandle *fh = notification.object;
NSLog(@"fh: %@", fh);
NSString *output = [[NSString alloc] initWithData:[fh readDataToEndOfFile] encoding:NSUTF8StringEncoding];
NSLog(@"output: '%@'", output);
}
@end
로그 : 나는 동기 또는 https://stackoverflow.com/a/16274541/1015200의 방법을 사용하여 그것을 할 경우
2014-12-16 10:19:58.154 SubProcess2[14893:704393] launching...
2014-12-16 10:19:58.165 SubProcess2[14893:704393] fh: <NSConcreteFileHandle: 0x6080000e9e80>
2014-12-16 10:19:58.165 SubProcess2[14893:704393] output: ''
2014-12-16 10:19:58.166 SubProcess2[14893:704393] finished.
나는 일을 얻을 수 있습니다. 다른 모든 기술 및 변형 (예 : performSelectorInBackground가없는 시작 작업)이 실패했습니다. 알림을 사용하여 제대로 작동하는지 확인하고 싶습니다. 그래서 도움이된다면 좋을 것 같습니다.
가 완벽하게 작동, 감사합니다! –