在Swift中调用外部命令 [英] Calling an external command in Swift

查看:223
本文介绍了在Swift中调用外部命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I call an external command (launch a subprocess) from a Swift script?

也许在Python中类似call(["ls", "-l"])之类的东西.

Perhaps something like call(["ls", "-l"]) in Python.

推荐答案

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

You can still use NSTask in Swift. Your example would be something like this.

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()

这篇关于在Swift中调用外部命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆