开发者

Cocoa NSTask help

I need a bit of help with NSTask. Also, I am new to Cocoa / Obj-C programming, 开发者_StackOverflowso 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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜