NSTask plutil无法识别的选项

时间:2013-04-15 08:46:23

标签: cocoa plist nstask

我正在尝试使用NSTask将二进制plist转换为xml,尽管遇到了一个我不太明白的错误。如果我接受命令NSTask失败并将其复制到命令行它就可以正常工作。希望有人可以告诉我出了什么问题。

NSString *defaultPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Preferences/com.defaults.plist"];

task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/plutil"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"-convert xml1", defaultPath, nil];

// using `@"-convert", "xml1", defaultPath, nil` doesn't seem to work either.

[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

[task waitUntilExit];
[task release];

的NSLog

/usr/bin/plutil (
    "-convert xml1",
    "/Users/Mira/Library/Preferences/com.defaults.plist"
)
unrecognized option: -convert xml1

2 个答案:

答案 0 :(得分:1)

NSTask格式化shell命令的方式可能有点棘手。这是一个应该有所帮助的建议。

[task setLaunchPath:@"/bin/bash"];

NSMutableArray *commandLine = [NSMutableArray new];
[commandLine addObject:@"-c"];
[commandLine addObject:@"/usr/bin/plutil -convert xml1"];
[commandLine addObject:defaultPath]; 

如果这不起作用,则创建一个plutil命令和文件路径的长字符串,并将其作为-c之后的第二个对象添加到命令行。

前:

NSString *cmd = [NSString stringWithFormat:@"/usr/bin/plutil -convert xml1 %@",defaultPath];

然后将其添加到数组

[task setLaunchPath:@"/bin/bash"];

NSMutableArray *commandLine = [NSMutableArray new];
[commandLine addObject:@"-c"];
[commandLine addObject:cmd];
希望有所帮助。祝你好运!

答案 1 :(得分:0)

“ - convert”和“xml 1”需要是单独的字符串