小编典典

以编程方式重启OSX应用

swift

我需要重新启动我的应用程序,以防万一我重新加载需要从一开始就开始的内容。我试过了

  let path = NSBundle.mainBundle().resourcePath!.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent
  let task = NSTask()
  task.launchPath = "open"
  task.arguments = [path]
  task.launch()
  exit(0)

但我收到一个错误 open

启动路径不可访问


阅读 218

收藏
2020-07-07

共1个答案

小编典典

尽管问题本身是微不足道的(忘记了路径),但在其他人需要相同功能的情况下,我还是留下了疑问。

let path = NSBundle.mainBundle().resourcePath!.stringByDeletingLastPathComponent.stringByDeletingLastPathComponent
let task = NSTask()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
exit(0)

编辑 (Sw3的每日Swift语法更改; Sw4也适用):

let url = URL(fileURLWithPath: Bundle.main.resourcePath!)
let path = url.deletingLastPathComponent().deletingLastPathComponent().absoluteString
let task = Process()
task.launchPath = "/usr/bin/open"
task.arguments = [path]
task.launch()
exit(0)
2020-07-07