Ios 如何在Swift 4中执行终端命令?
我有一个iOS应用程序,在单元测试(XCTests)中,我需要执行一些终端命令,例如Ios 如何在Swift 4中执行终端命令?,ios,swift,bash,shell,terminal,Ios,Swift,Bash,Shell,Terminal,我有一个iOS应用程序,在单元测试(XCTests)中,我需要执行一些终端命令,例如 xcrun simctl shutdown[模拟器UDID] 我创建这个函数是为了运行shell脚本 func commandLine(launchPath: String, arguments: [String]) -> String { // Used to be NSTask() before Swift 3 let process = Process() // Set t
xcrun simctl shutdown[模拟器UDID]
我创建这个函数是为了运行shell脚本
func commandLine(launchPath: String, arguments: [String]) -> String {
// Used to be NSTask() before Swift 3
let process = Process()
// Set the task parameters
process.launchPath = "/usr/bin/env"
process.arguments = ["pwd"]
// Create a Pipe and make the process
// put all the output there
let pipe = Pipe()
process.standardOutput = pipe
// Launch the process
process.launch()
// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: NSUTF8StringEncoding)
print(output!)
}
然而,我的错误是这样说的
使用未解析的标识符“进程”
进行了一些研究,结果发现在Swift 4中,Process()
已重命名为CommandLine
,但更改为CommandLine
会导致不同的错误
无法构造“命令行”,因为它没有可访问的初始值设定项
您无法在iOS中启动进程。在每个单元测试之前和之后,是否有其他方法执行终端命令?预操作(可以在方案编辑器中配置)如何?顺便说一句:由于链接,进程重命名为CommandLine,而NSTask重命名为Process(from)。pre操作在scheme之前运行,而我正在寻找一种在每个单元测试之前运行的方法。