I wanted to display output from Process() to NSTextView in real time. Below are my code snippets:
The above code snippets only works in debug mode. Nothing displays on the NSTextView besides the initial text when it's in release mode.
Code Block func displayOutput (_ task:Process) { let outputPipe = Pipe() setvbuf(stdout, nil, _IONBF, 0) dup2(outputPipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO) task.standardOutput = outputPipe outputPipe.fileHandleForReading.readabilityHandler = { pipe in if let outputString = String(data: pipe.availableData, encoding: String.Encoding.utf8) { if outputString.count > 0 { DispatchQueue.main.async(execute: { print(outputString, separator: "\n", to: &self.outputTextView.string) let range = NSRange(location:self.outputTextView.string.count,length:0) self.outputTextView.scrollRangeToVisible(range) } } } } }
The above code snippets only works in debug mode. Nothing displays on the NSTextView besides the initial text when it's in release mode.