【发布时间】:2018-12-21 07:36:36
【问题描述】:
我有以下代码并想导出结果
self.logger.string += line
到一个文件。怎么办?
func syncShellExec(path: String, args: [String] = []) {
//let script = [path!]
let process = Process()
process.launchPath = "/bin/bash"
process.arguments = [path] + args
let outputPipe = Pipe()
let filelHandler = outputPipe.fileHandleForReading
process.standardOutput = outputPipe
filelHandler.readabilityHandler = { pipe in
let data = pipe.availableData
if let line = String(data: data, encoding: .utf8) {
// Update your view with the new text here
// Bounce back to the main thread to update the UI
DispatchQueue.main.async {
self.logger.string += line
}
} else {
print("Error decoding data: \(data.base64EncodedString())")
}
}
process.launch()
process.waitUntilExit()
filelHandler.readabilityHandler = nil
//self.loggerScroll.flashScrollers()
}
【问题讨论】: