简体   繁体   English

可可NSTask帮助

[英]Cocoa NSTask help

I need a bit of help with NSTask. 我需要NSTask的帮助。 Also, I am new to Cocoa / Obj-C programming, so please bear with me. 另外,我是Cocoa / Obj-C编程的新手,所以请多多包涵。 I am trying to make a directory. 我正在尝试建立目录。 Then, remove it. 然后,将其删除。 So here is what I have so far: 所以这是我到目前为止所拥有的:

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];

My question is if the part towards the end about "release"-ing is correct? 我的问题是,有关“释放”的最后部分是否正确? If not, can someone help me correct it? 如果没有,有人可以帮我改正吗? Also, if I wanted to do another NSTask of "rmdir", would I just do "task = [[NSTask alloc] init];" 另外,如果我要执行另一个“ rmdir”的NSTask,我是否要执行“ task = [[NSTask alloc] init];”? and so on for each variable I used or will I need to make new variables? 以此类推,对于我使用的每个变量,还是需要创建新变量? THANKS A LOT! 非常感谢!

First, no, you aren't managing memory correctly (hint: only task is correctly handled above). 首先,不,您没有正确管理内存(提示:上面仅正确处理了task )。 Read this as it explains all. 请阅读此内容,以解释所有内容。

Secondly, there is no need to use an NSTask instance to make/delete a directory. 其次,无需使用NSTask实例来创建/删除目录。 You should use NSFileManager instead; 您应该使用NSFileManager代替; again -- the documentation explains all. 再次- 文档说明了所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM