Creating process with arguments in Swift?

醉酒当歌 提交于 2021-01-28 20:49:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!