如何从Swift执行外部程序?

时间:2021-04-05 21:52:08

I am new in Swift and I did not found anything about executing external programs or access external processes using Swing language.

我是Swift新手,我没有发现任何关于执行外部程序或使用Swing语言访问外部进程的信息。

Is it possible to do in the current stage of the language development or I should use Objective-C instead?

是否有可能在语言发展的当前阶段,或者我应该使用Objective-C ?

Maybe there are some Objective-C libraries that can be used inside my Swift program?

也许有一些Objective-C库可以在我的Swift程序中使用?

Thanks.

谢谢。

2 个解决方案

#1


13  

You can run external programs using NSTask. For example, from Circle and Square:

您可以使用NSTask运行外部程序。例如,从圆形和方形:

import Foundation

func executeCommand(command: String, args: [String]) -> String {

    let task = NSTask()

    task.launchPath = command
    task.arguments = args

    let pipe = NSPipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)

    return output        
}

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"])
println("Command output: \(commandOutput)")

#2


2  

Improved version of Rob's answer (in that you don't need to specify the full path of your executable), and also updated for Swift 3:

Rob的答案的改进版本(因为您不需要指定可执行文件的完整路径),并且还更新了Swift 3:

import Foundation

func execCommand(command: String, args: [String]) -> String {
    if !command.hasPrefix("/") {
        let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return execCommand(command: commandFull, args: args)
    } else {
        let proc = Process()
        proc.launchPath = command
        proc.arguments = args
        let pipe = Pipe()
        proc.standardOutput = pipe
        proc.launch()
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        return String(data: data, encoding: String.Encoding.utf8)!
    }
}

#1


13  

You can run external programs using NSTask. For example, from Circle and Square:

您可以使用NSTask运行外部程序。例如,从圆形和方形:

import Foundation

func executeCommand(command: String, args: [String]) -> String {

    let task = NSTask()

    task.launchPath = command
    task.arguments = args

    let pipe = NSPipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: NSUTF8StringEncoding)

    return output        
}

let commandOutput = executeCommand("/bin/echo", ["Hello, I am here!"])
println("Command output: \(commandOutput)")

#2


2  

Improved version of Rob's answer (in that you don't need to specify the full path of your executable), and also updated for Swift 3:

Rob的答案的改进版本(因为您不需要指定可执行文件的完整路径),并且还更新了Swift 3:

import Foundation

func execCommand(command: String, args: [String]) -> String {
    if !command.hasPrefix("/") {
        let commandFull = execCommand(command: "/usr/bin/which", args: [command]).trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
        return execCommand(command: commandFull, args: args)
    } else {
        let proc = Process()
        proc.launchPath = command
        proc.arguments = args
        let pipe = Pipe()
        proc.standardOutput = pipe
        proc.launch()
        let data = pipe.fileHandleForReading.readDataToEndOfFile()
        return String(data: data, encoding: String.Encoding.utf8)!
    }
}