[英]How to launch an external Process?
I am reading through the docs of swift and came across Type Methods. 我正在阅读swift的文档并遇到了类型方法。 For example here: https://developer.apple.com/documentation/foundation/process 例如: https : //developer.apple.com/documentation/foundation/process
The offered Type Method is: 提供的类型方法是:
class func run(URL, arguments: [String], terminationHandler: ((Process) -> Void)? = nil)
How can I use this in my code? 我如何在我的代码中使用它? For example when I press a button? 例如,当我按下按钮? How can I add a clean-up function to the terminationHandler? 如何向terminationHandler添加清理功能?
In a macos
app, you may use run
for launching external processes, an example might be: 在macos
应用程序中,您可以使用run
来启动外部进程,例如:
1) one-shot execution: 1)一次性执行:
let url = URL(fileURLWithPath:"/bin/ls")
do {
try Process.run(url, arguments: []) { (process) in
print("\ndidFinish: \(!process.isRunning)")
}
} catch {}
2) you may want to use a Process
instance to be able to setup more comfortably its behaviour, doing so: 2)您可能希望使用Process
实例来更舒适地设置其行为,这样做:
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/ls")
process.arguments = ["-la"]
process.terminationHandler = { (process) in
print("\ndidFinish: \(!process.isRunning)")
}
do {
try process.run()
} catch {}
So I did launch the ls
command (you may check your console for the result), then in the closure terminationHandler
I'm getting back such process. 所以我确实启动了ls
命令(你可以检查你的控制台的结果),然后在关闭的terminationHandler
我得到了这样的过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.