【发布时间】:2015-12-22 16:49:18
【问题描述】:
我正在尝试在 mac (10.10) 上使用 swift 2 运行命令行工具:
let task = NSTask()
task.launchPath = "/path/to/wrong/binary"
task.launch()
// NSPipe() stuff to catch output
task.waitUntilExit()
// never reached
if (task.terminationStatus != 0){
NSLog("uh oh")
}
由于路径错误,我的程序以launch path not accessible 终止。但是,我不知道如何捕获此错误。在task.launch() 周围使用do { try } catch {} 不起作用,因为它不会抛出异常,查看terminationStatus 也不起作用,因为它永远不会到达。
我怎样才能发现错误的launchPath?
Apple Swift 版本 2.1.1 (swiftlang-700.1.101.15 clang-700.1.81) 目标:x86_64-apple-darwin14.5.0
【问题讨论】:
-
您可以在运行之前尝试检查启动路径中的文件是否存在。
标签: swift swift2 command-line-interface