2010-07-07 86 views
0

我需要一些NSTask的幫助。另外,我是Cocoa/Obj-C編程的新手,請耐心等待。我正在製作一個目錄。然後,將其刪除。所以這裏是我到目前爲止:可可NSTask幫助

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];」等等我爲每個變量使用或將需要做出新的變量?非常感謝!

回答

1

首先,不,你沒有正確管理內存(提示:只有task正確處理上面)。 Read this,因爲它解釋了所有。

其次,不需要使用NSTask實例來創建/刪除目錄。您應該使用NSFileManager來代替;再次 - the documentation解釋所有。

+0

其實我只是以mkdir/rmdir爲例。我將爲其他命令執行此操作,例如dd,dns-sd,asr等。 – hassaanm 2010-07-07 17:27:05