1

I'm trying to get a separated standardError output and standardOutput string but I also need the full string. Following code will explain my case

combined/full string:

    let task = Process()
    task.launchPath = "/usr/sbin/killall"
    task.arguments = ["Dock"]
    let pipe = Pipe()
    task.standardOutput = pipe
    task.standardError = pipe
    task.launch()
    task.waitUntilExit()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let fulloutput: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String

    print(fulloutput)

Separated strings:

    let task = Process()
    task.launchPath = "/usr/sbin/killall"
    task.arguments = ["Dock"]
    let pipe = Pipe()
    let errorpipe = Pipe()
    task.standardOutput = pipe
    task.standardError = errorpipe
    task.launch()
    task.waitUntilExit()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let errordata = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
    let erroroutput: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String

    print(output)
    print(erroroutput)

How do I combine these 2 so I get an output like this without running the command twice?:

    print(output)
    print(erroroutput)
    print(fulloutput)

Thanks!

Steven B.
  • 1,429
  • 2
  • 19
  • 38
  • You can read asynchronously from both stdout and stderr (see e.g. http://stackoverflow.com/a/38088651/1187415) and then do whatever you want with the incoming data. But note that there is no guarantee the incoming chunks are complete lines or even complete UTF-8 sequences. – Martin R Jan 13 '17 at 09:12

0 Answers0