在Swift中调用外部命令

Rya*_*anM 11 exec swift

如何从Swift脚本调用外部命令(启动子进程)?

也许就像call(["ls", "-l"])在Python中一样.

Con*_*nor 19

你仍然可以在Swift中使用NSTask.你的例子就是这样的.

let task = NSTask()
task.launchPath = "/bin/ls"
task.arguments = ["-l"]
task.launch()
Run Code Online (Sandbox Code Playgroud)

Swift 3 +,macOS 10.13+

let task = Process()
task.executableURL = URL(fileURLWithPath: "/bin/ls")
task.arguments = ["-l"]
task.run()
Run Code Online (Sandbox Code Playgroud)