Execute a command in swift


/ Published in: Other
Save to your folder(s)

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"])
println("Command output: \(commandOutput)")


Copy this code and paste it in your HTML
  1. func executeCommand(command: String, args: [String]) -> String {
  2.  
  3. let task = NSTask()
  4.  
  5. task.launchPath = command
  6. task.arguments = args
  7.  
  8. let pipe = NSPipe()
  9. task.standardOutput = pipe
  10. task.launch()
  11.  
  12. let data = pipe.fileHandleForReading.readDataToEndOfFile()
  13. let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)
  14.  
  15. return output
  16.  
  17. }

URL: http://square-the-circle.com/2014/08/03/executing-a-system-command-from-a-macos-app-swift/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.