问题
Problem with doing process in Swift 3, It's not working, I click and nothing is happening.
let open = Process()
open.launchPath = "/usr/bin/openssl"
open.arguments = ["openssl enc -aes-256-cbc -d -in \"" + existing.stringValue +
"\" -out \"" + new.stringValue + "/" + name.stringValue + "\""]
open.launch()
open.waitUntilExit()
How do I create a process with arguments in Swift?
回答1:
With this function, you can pass the arguments as a string.
func shell(at: String, _ args: String) {
let task = Process()
task.launchPath = at
task.arguments = ["-c", args]
let pipeStandard = Pipe()
task.standardOutput = pipeStandard
task.launch()
let dataStandard = pipeStandard.fileHandleForReading.readDataToEndOfFile()
let outputStandard = String(data: dataStandard, encoding: String.Encoding.utf8)!
if outputStandard.count > 0 {
let lastIndexStandard = outputStandard.index(before: outputStandard.endIndex)
print(String(outputStandard[outputStandard.startIndex ..< lastIndexStandard]))
}
task.waitUntilExit()
}
来源:https://stackoverflow.com/questions/40422406/creating-process-with-arguments-in-swift