[英]Running NSTask inside a loop in Objective-C
我试图运行一个简单的shell命令,该命令在NSArray生成的编译时在不确定大小的循环内相当快地运行并返回文本。 在像perl这样的脚本语言中,我将能够执行以下操作:
for(i=0;i<=$myinputarraysize;i++){
$output[i]=`/my/task $inputarray[i]`;
}
这将从任务的预期输出为我构建一个新数组。 在Obj-C中,这似乎要困难得多,并且让我感到困惑。 现在我的循环看起来像这样:
for(int i=0; i<[inputarray count]; i++){
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath:nsdchat];
NSArray *args;
args = [NSArray arrayWithObjects:@"/my/task", [inputarray objectAtIndex:i], nil];
[task setArguments:args];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *desc;
desc = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
desc = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
[descriptions insertObject:desc atIndex:i];
[task release];
[args release];
[pipe release];
[file release];
[data release];
}
我的目标是用任务的输出(我知道总是一个字符串,并且总是以我要删除的换行符结尾)填充描述(一个NSMutableArray)。 似乎我缺少有关内存释放的信息,因为当我运行此命令并输出NSLog时,整个循环的计数结果相同。
有没有更简单或更简单的方法循环遍历这样的简单任务? 我是否为自己过于复杂?
首先,不需要在循环内完成大部分工作。 task
, pipe
和file
看起来都可以在循环外处理。 您还应该考虑使用Objective-C 2.0点语法和快速枚举来减少一些麻烦。
更重要的是:
NSArray *args;
args = [NSArray arrayWithObjects:@"/my/task", [inputarray objectAtIndex:i], nil];
[task setArguments:args];
这表示传递到路径ndschat
的可执行文件的第一个参数是/my/task
。 这似乎与您的PERL使用情况不符。 可能您只想:
NSArray *args;
args = [NSArray arrayWithObject:[inputarray objectAtIndex:i]];
[task setArguments:args];
或者,考虑到样式注释:
for(NSString *argument in inputarray)
{
...
task.arguments = [NSArray arrayWithObject:argument];
...
}
编辑:您还释放了许多您不拥有的对象,以及在代码中增加大量内容是可能导致崩溃的内存管理错误。 因此,要减少整个故障并纠正该故障:
for(NSString *argument in inputarray)
{
NSTask *task = [[NSTask alloc] init]; // you now own this
task.launchPath = nsdchat;
NSPipe *pipe = [NSPipe pipe]; // you don't own this
task.standardOutput = pipe;
NSFileHandle *file = [pipe fileHandleForReading]; // you also don't own this
task.arguments = [NSArray arrayWithObject:argument];
[task launch];
[task waitUntilExit]; // you should wait until the task is done
NSData *data = [file readDataToEndOfFile]; // this is another thing
// you don't own. Note also that
// readDataToEndOfFile advances
// the current read pointer, so
// it should be fine to do this
// successively
NSString *desc;
desc = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
autorelease];
// you don't own this because
// of the autorelease; you
// don't want to own it since
// the next line will throw
// it away
desc = [string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
// you don't own this
[descriptions addObject:desc];
[task release];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.