我如何从Swift脚本中调用外部命令(启动子进程)?
可能类似于Python中的call(["ls", "-l"])
。
您仍然可以在Swift中使用NSTask。您的示例将类似于以下内容。
let task = NSTask()
task.launchPath = "/bin/ls"
task.arguments = ["-l"]
task.launch()
Swift 3+、macOS 10.13+
let task = Process()
task.executableURL = URL(fileURLWithPath: "/bin/ls")
task.arguments = ["-l"]
task.run()
Process
。 - Raphael