This line of code:
这行代码:
system("/Applications/Xcode.app/Contents/Developer/usr/bin/opendiff /Users/LukeSkywalker/Documents/doc1.rtf /Users/LukeSkywalker/Documents/doc2.rtf");
gives me this warning:
给我这个警告:
'system' is deprecated: first deprecated in iOS 8.0 - Use posix_spawn APIs instead.
I've read a little bit about posix_spawn, but I can't figure out what an equivalent line of code using posix_spawn would look like.
我已经读了一些关于posix_spawn的内容,但是我不知道使用posix_spawn的代码是什么样子的。
Any help or links to samples would be appreciated.
任何对样品的帮助或链接都会被欣赏。
2 个解决方案
#1
12
Using posix_spawn()
, to answer your question:
使用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);
Or, you could use NSTask:
或者,你可以用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];
If you don't need it to be synchronous, just remove the call to waitpid()
(make sure to call it somewhere else, or you'll end up with a zombie process until your app exits) or [task waitUntilExit]
.
如果您不需要它是同步的,那么只需删除waitpid()的调用(确保在其他地方调用它,或者在应用程序退出之前,您将结束一个僵尸进程)或[任务waitUntilExit]。
#2
1
Swift 3, Xcode 8.3.1
斯威夫特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()
}
#1
12
Using posix_spawn()
, to answer your question:
使用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);
Or, you could use NSTask:
或者,你可以用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];
If you don't need it to be synchronous, just remove the call to waitpid()
(make sure to call it somewhere else, or you'll end up with a zombie process until your app exits) or [task waitUntilExit]
.
如果您不需要它是同步的,那么只需删除waitpid()的调用(确保在其他地方调用它,或者在应用程序退出之前,您将结束一个僵尸进程)或[任务waitUntilExit]。
#2
1
Swift 3, Xcode 8.3.1
斯威夫特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()
}