Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/112.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift 4中执行终端命令?_Ios_Swift_Bash_Shell_Terminal - Fatal编程技术网

Ios 如何在Swift 4中执行终端命令?

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

我有一个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 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之前运行,而我正在寻找一种在每个单元测试之前运行的方法。