在NSTask中使用launchctl

RLT*_*RLT 3 macos cocoa objective-c launchd nstask

我想launchctl从应用程序执行.

为此,我使用以下代码,

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/launchctl"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"load ", @"/Users/XYZ/com.XYZ.plist", nil];
[task setArguments: arguments];

[task launch];
Run Code Online (Sandbox Code Playgroud)

它给了我错误, launchctl: unknown subcommand "load "

但是,当我从终端运行命令时,它正确执行

>launchctl load /Users/XYZ/com.XYZ.plist 
Run Code Online (Sandbox Code Playgroud)

这有什么区别,它如何运作NSTask?

Mar*_*n R 7

删除中的尾随空格@"load ".

数组中的每个对象都是该任务的单个参数.无需添加空格来分隔参数(或引用参数).