Objective c n按换行符拆分的任务数据\n
我是objective-C的新手,如果我遗漏了什么,请原谅我。但我们都必须从某个地方开始:) 我有一段代码是从另一个开源项目获得的,它执行一个命令并将结果传递给另一个方法。我需要做的是倾听打印到stdout的每一行,并对每一行做一些事情 我正在使用的代码段如下所示:Objective c n按换行符拆分的任务数据\n,objective-c,event-handling,nstask,Objective C,Event Handling,Nstask,我是objective-C的新手,如果我遗漏了什么,请原谅我。但我们都必须从某个地方开始:) 我有一段代码是从另一个开源项目获得的,它执行一个命令并将结果传递给另一个方法。我需要做的是倾听打印到stdout的每一行,并对每一行做一些事情 我正在使用的代码段如下所示: NSMutableArray *args = [NSMutableArray array]; NSString *input = [details valueForKey:@"input"]; for
NSMutableArray *args = [NSMutableArray array];
NSString *input = [details valueForKey:@"input"];
for (NSString *i in [input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]) {
[args addObject:i];
}
NSTask *scriptTask = [NSTask new];
NSPipe *outputPipe = [NSPipe pipe];
if ([_NSFileManager() isExecutableFileAtPath:scriptPath] == NO) {
NSArray *chmodArguments = @[@"+x", scriptPath];
NSTask *chmod = [NSTask launchedTaskWithLaunchPath:@"/bin/chmod" arguments:chmodArguments];
[chmod waitUntilExit];
}
[scriptTask setStandardOutput:outputPipe];
[scriptTask setLaunchPath:scriptPath];
[scriptTask setArguments:args];
NSFileHandle *filehandle = [outputPipe fileHandleForReading];
[scriptTask launch];
[scriptTask waitUntilExit];
NSData *outputData = [filehandle readDataToEndOfFile];
NSString *outputString = [NSString stringWithData:outputData encoding:NSUTF8StringEncoding];
if (NSObjectIsNotEmpty(outputString)) {
[self.world.iomt inputText:outputString command:IRCPrivateCommandIndex("privmsg")];
}
task = new Task("ls");
task.addListener("newline", function(data) {
somethingElse("sendmsg", data);
});
task.run();
因此,我需要等待命令打印到stdout的每一行新行,而不是等待进程完成然后对结果进行处理
我的背景主要是web开发人员,因此我想如果您使用Node.js和事件发射器,我的目标将类似于以下内容:
NSMutableArray *args = [NSMutableArray array];
NSString *input = [details valueForKey:@"input"];
for (NSString *i in [input componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]) {
[args addObject:i];
}
NSTask *scriptTask = [NSTask new];
NSPipe *outputPipe = [NSPipe pipe];
if ([_NSFileManager() isExecutableFileAtPath:scriptPath] == NO) {
NSArray *chmodArguments = @[@"+x", scriptPath];
NSTask *chmod = [NSTask launchedTaskWithLaunchPath:@"/bin/chmod" arguments:chmodArguments];
[chmod waitUntilExit];
}
[scriptTask setStandardOutput:outputPipe];
[scriptTask setLaunchPath:scriptPath];
[scriptTask setArguments:args];
NSFileHandle *filehandle = [outputPipe fileHandleForReading];
[scriptTask launch];
[scriptTask waitUntilExit];
NSData *outputData = [filehandle readDataToEndOfFile];
NSString *outputString = [NSString stringWithData:outputData encoding:NSUTF8StringEncoding];
if (NSObjectIsNotEmpty(outputString)) {
[self.world.iomt inputText:outputString command:IRCPrivateCommandIndex("privmsg")];
}
task = new Task("ls");
task.addListener("newline", function(data) {
somethingElse("sendmsg", data);
});
task.run();
希望你能理解我的目标。谢谢 您可以做的是向NSFileHandle添加一个观察者,以便在它读取某些内容时通知您 例如:
[fileHandler readInBackgroundAndNotify];
//Need to set NSTask output pipes
[self.task setStandardOutput: outputPipe];
[self.task setStandardError: outputPipe];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(readFromTask:) name:NSFileHandleReadCompletionNotification object:fileHandler];
-(void)readFromTask:(NSNotification *)aNotifcation;
{
NSData *data = [[aNotifcation userInfo] objectForKey: NSFileHandleNotificationDataItem];
if (data != 0) {
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// Do something with the text
[text release];
[[aNotifcation object] readInBackgroundAndNotify];
}
}
然后有一个通知,你可以添加到任务中,以指示任务何时完成,这样你就可以进行清理
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskEnded:) name:NSTaskDidTerminateNotification object:task];
- (void)taskEnded:(NSNotification *) aNotifcation
{
NSData *data = [[aNotifcation userInfo] objectForKey: NSFileHandleNotificationDataItem];
if (data != 0) {
NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[text release];
}
[self.task release];
}
因此,您可以从代码中删除:
[scriptTask waitUntilExit];
并将数据处理移到通知中
NSData *outputData = [filehandle readDataToEndOfFile];
NSString *outputString = [NSString stringWithData:outputData encoding:NSUTF8StringEncoding];
if (NSObjectIsNotEmpty(outputString)) {
[self.world.iomt inputText:outputString command:IRCPrivateCommandIndex("privmsg")];
}
这是一个粗略的想法,在