Ich mache eine OS X App, die ein Shell-Skript ausführen muss. Hier sind meine SWIFT-Code:NSTask/bin/echo:/bin/echo: binäre Datei kann nicht ausgeführt werden
func runTask(arguments: [String]) {
output.string = ""
task = NSTask()
task.launchPath = "/bin/bash"
task.arguments = arguments;
errorPipe = NSPipe()
outputPipe = NSPipe()
task.standardError = errorPipe
task.standardOutput = outputPipe
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardOutput!.fileHandleForReading)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didCompleteReadingFileHandle(_:)), name: NSFileHandleReadCompletionNotification, object: task.standardError!.fileHandleForReading)
errorPipe.fileHandleForReading.readInBackgroundAndNotify()
outputPipe.fileHandleForReading.readInBackgroundAndNotify()
task.launch()
}
func didCompleteReadingFileHandle(sender: NSNotification) {
let data: NSData = sender.userInfo![NSFileHandleNotificationDataItem] as! NSData;
let string = NSString(data: data, encoding: NSUTF8StringEncoding)!
// The output property is a NSTextView object
output.string?.appendContentsOf(String(string))
}
Jetzt habe ich versucht, die runTask
Methode aufrufen:
runTask(["/bin/echo", "1234"])
Es sagt der folgende Fehler:
/bin/echo: /bin/echo: cannot execute binary file
Nun ging ich zurück in das Terminal und tippte echo 1234
läuft es ohne Probleme perfekt, wie funktioniert das jetzt? Vielen Dank.