可可 NSTask 帮助

发布于 2024-09-08 07:05:28 字数 1015 浏览 7 评论 0原文

我需要一些有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜光 2024-09-15 07:05:28

首先,不,您没有正确管理内存(提示:上面只有 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 use NSFileManager instead; again -- the documentation explains all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文