I wrote a simple OSx (10.11) application to execute shell commands when a button is pressed. It works when I run it from xcode, but when I export the application via \"archive\"
Your issue is the result of two things happening together:
you return default values
you don't specify alternative branches for the control flow
What happens is that it hides potential failures, and leads to code that is very hard to debug, as you experienced.
A possible solution with your existing code is to cover all possible ways, meaning providing else
branches to your if var string = String.fromCString(UnsafePointer(errdata.bytes))
conditions, where you will handle errors.
Thanks to Eric D. I simplified my code and now everything is working.
func runCommand(path : String, args : [String]) -> (output: NSString, error: NSString, exitCode: Int32) {
let task = NSTask()
task.launchPath = path
task.arguments = args
let outpipe = NSPipe()
task.standardOutput = outpipe
let errpipe = NSPipe()
task.standardError = errpipe
task.launch()
let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: outdata, encoding: NSUTF8StringEncoding)
let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
let error_output = NSString(data: errdata, encoding: NSUTF8StringEncoding)
task.waitUntilExit()
let status = task.terminationStatus
return (output!, error_output!, status)
}