【问题标题】:Swift - Why Process.arguments returns empty arraySwift - 为什么 Process.arguments 返回空数组
【发布时间】:2016-05-25 20:02:17
【问题描述】:

我正在我的 iOS 应用程序中进行一些 UI 测试,但我看到了一些奇怪的行为。

在我的setUp() 方法中,我向XCUIApplication().launchArguments 添加了一些值,但是当我查询以查看启动参数时,我得到了一个空数组。

这就是我的setUp() 方法的样子:

override func setUp() {
    super.setUp()

    let application = XCUIApplication()
    application.launchArguments = ["USE_SERVER_DEBUG"]
    application.launch()
}

这是调用Process.arguments 来检索参数的函数

func checkArguments(){
    let launchArguments = Process.arguments
    for index in 0 ..< launchArguments.count {
        let argument = launchArguments[index] as String

        if argument.compare("USE_DEBUG_SERVER") == NSComparisonResult.OrderedSame {
            // Do something
        }
    }
    return true
}

【问题讨论】:

标签: ios swift unit-testing xcode-ui-testing


【解决方案1】:

我不确定Process 是您自己的课程还是拼写错误,但您应该使用NSProcessInfo

if NSProcessInfo.processInfo().arguments.contains("USE_SERVER_DEBUG") {
    // Do something
}

另外,我通常建议不要设置启动参数。相反,您应该将它们附加。但这更像是一种最佳实践。

application.launchArguments += "USE_SERVER_DEBUG"

【讨论】:

  • Process 是 Swift 的“当前进程的命令行参数”。
猜你喜欢
  • 2021-08-05
  • 2018-12-19
  • 2021-09-15
  • 2012-08-26
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多