在Swift中使用参数创建进程?

时间:2016-11-04 12:13:02

标签: swift xcode process openssl command

在Swift 3中执行过程时出现问题,它无法正常工作,我点击了,没有任何事情发生。

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()

如何在Swift中创建带参数的进程?

1 个答案:

答案 0 :(得分:3)

使用此功能,您可以将参数作为字符串传递。

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()
}