Swift / Xojo: Shells

A question for someone who speaks both Swift and Xojo…

As shell objects in Xojo can be run both synchronously and asynchronously, how can a Swift Process() be run the same way?

With Xojo I can run a shell and respond accordingly when data is received from the process being run, thus not locking up the UI during long processes.

With Swift I only know how to run a Process() and handle the resulting data in one lump.

Can anyone who is familiar with Swift Process() offer me any insights?


well I have only been using Shell via Swift for a short time, using this code

func shell(_ command: String ) -> String {
    var output  : String = ""
    let task    = Process()
    let pipe    = Pipe()
    task.executableURL  = URL(fileURLWithPath: "/bin/bash")
    task.arguments      = ["-c", "\(command)"]
    task.standardOutput = pipe
    task.standardError  = pipe
    do {
        try task.run()
        let handle = pipe.fileHandleForReading
        let data   = handle.readDataToEndOfFile()
        output     = String(data: data, encoding: .utf8) ?? "no result"
    } catch {
       output = "'command' failed."

    return output