我需要一点关于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];我的问题是,最后关于“发布”-ing的部分是否正确?如果没有,有人能帮我改正吗?另外,如果我想对"rmdir“执行另一个任务,我会对我使用的每个变量执行”NSTask = [NSTask alloc init];“,依此类推,还是需要创建新的变量?非常感谢!
发布于 2010-07-07 13:51:50
首先,不,您没有正确地管理内存(提示:上面只正确地处理了task )。Read this,因为它解释了一切。
其次,不需要使用NSTask实例来创建/删除目录。您应该改用NSFileManager;同样,-- the documentation解释了一切。
https://stackoverflow.com/questions/3191202
复制相似问题