我需要一些关于 NSTask 的帮助。另外,我是 Cocoa / Obj-C 编程的新手,所以请多多包涵。我正在尝试创建一个目录。然后,将其移除。所以这是我到目前为止所拥有的:
NSLog (@"START");
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/mkdir"];
NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"/tmp/TEMP", nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task setStandardError: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSLog (@"MKDIR");
[task launch];
[task waitUntilExit];
NSData *data;
data = [file readDataToEndOfFile];
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"OUTPUT:\n%@", string);
[task release];
//EDIT: The following lines should be removed and [string release]; should be added.
[arguments release];
[pipe release];
[file release];
[data release];
我的问题是,最后关于“释放”的部分是否正确?如果没有,有人可以帮我纠正吗?另外,如果我想做另一个“rmdir”的NSTask,我会做“task = [[NSTask alloc] init];”吗?等等我使用的每个变量还是我需要创建新变量?多谢!