9

I need to restart my app in case I reload something that will require a start from the very beginning. I tried this

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

but I get an error upon the open

launch path not accessible

qwerty_so
  • 35,448
  • 8
  • 62
  • 86

1 Answers1

18

Though the problem itself was trivial (forgot the path) I leave question and answer in case someone else needs the same functionality.

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

Edit (daily Swift syntax change for Sw3; works also for 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)
qwerty_so
  • 35,448
  • 8
  • 62
  • 86
  • Thought I might add some odd happening. Have been using tasks and bin/bash commands through my program for awhile. Haven't been able to run the commands through my xcode without exporting the project entirely. Adding this however, somehow fixed it? I can run the previous commands now as well as this one, so thanks for that too! I'm guessing this was a permission issue but it still doesn't add up much. **Edit** - this is _after_ I relaunch the application. Still odd. – Dylan Nov 09 '16 at 17:22
  • When testing tha above in single step mode it did not work, but without break it sent the app into an endless loop (like expected). Stopping XCode also terminated the app (probably because it was regarded to be a child process). This code is a bit of borderline. – qwerty_so Nov 09 '16 at 17:28