2010-07-07 5 views
0

NSTask와 관련하여 약간의 도움이 필요합니다. 또한, 나는 코코아/Obj-C 프로그래밍에 익숙하지 않으므로 제발 참아주십시오. 나는 디렉토리를 만들려고 노력하고있다. 그런 다음 제거하십시오. 그래서 여기까지 제가 가지고있는 것입니다 :코코아 NSTask help

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