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

2

1 Answer 1

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)
2
  • 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 9, 2016 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 9, 2016 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.