如何从Swift执行外部程序? [英] How to execute external program from Swift?
本文介绍了如何从Swift执行外部程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是Swift的新手,我没有发现任何有关使用Swing语言执行外部程序或访问外部进程的信息.
I am new in Swift and I did not found anything about executing external programs or access external processes using Swing language.
在语言开发的当前阶段是否有可能做,还是应该改用Objective-C?
Is it possible to do in the current stage of the language development or I should use Objective-C instead?
也许我的Swift程序中可以使用一些Objective-C库?
Maybe there are some Objective-C libraries that can be used inside my Swift program?
谢谢.
推荐答案
您可以使用NSTask
运行外部程序.例如,来自圆形和方形:
You can run external programs using NSTask
. For example, from Circle and Square:
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)")
这篇关于如何从Swift执行外部程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文