可可 NSTask 帮助
我需要一些有关 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];”对于我使用的每个变量等等,或者我需要创建新变量吗?多谢!
I need a bit of help with NSTask. Also, I am new to Cocoa / Obj-C programming, so please bear with me. 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];" and so on for each variable I used or will I need to make new variables? THANKS A LOT!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,不,您没有正确管理内存(提示:上面只有
task
被正确处理)。 阅读本文,因为它解释了所有内容。其次,不需要使用 NSTask 实例来创建/删除目录。你应该使用 NSFileManager 来代替;再次 - 文档< /a> 解释了一切。
First, no, you aren't managing memory correctly (hint: only
task
is correctly handled above). Read this as it explains all.Secondly, there is no need to use an
NSTask
instance to make/delete a directory. You should useNSFileManager
instead; again -- the documentation explains all.