下面这行代码:
system("/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff /Users/LukeSkywalker/Documents/doc1.rtf /Users/LukeSkywalker/Documents/doc2.rtf");给我这样的警告:
'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs instead.我读过一些关于posix_spawn的文章,但我想不出使用posix_spawn的等效代码行是什么样子的。
任何帮助或链接到样本将不胜感激。
发布于 2015-01-05 23:03:42
使用posix_spawn()回答您的问题:
#include <spawn.h>
extern char **environ;(...)
pid_t pid;
char *argv[] = {
"/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff",
"/Users/LukeSkywalker/Documents/doc1.rtf",
"/Users/LukeSkywalker/Documents/doc2.rtf",
NULL
};
posix_spawn(&pid, argv[0], NULL, NULL, argv, environ);
waitpid(pid, NULL, 0);或者,您可以使用NSTask:
NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff";
task.arguments = [NSArray arrayWithObjects:
@"/Users/LukeSkywalker/Documents/doc1.rtf",
@"/Users/LukeSkywalker/Documents/doc2.rtf",
nil];
[task launch];
[task waitUntilExit];如果不需要同步,只需删除对waitpid() (确保在其他地方调用它,否则将以僵尸进程结束,直到应用程序退出)或[task waitUntilExit]的调用。
发布于 2017-11-08 08:58:58
Swift 3,Xcode 8.3.1
func system(_ command: String) {
var args = command.components(separatedBy: " ")
let path = args.first
args.remove(at: 0)
let task = Process()
task.launchPath = path
task.arguments = args
task.launch()
task.waitUntilExit()
}https://stackoverflow.com/questions/27046728
复制相似问题