swift - Xcode 代码中无法识别的其他 Swift 标志

标签 swift xcode

我有一个 Swift 项目,我向它添加了一个单元测试目标。 我的配置文件 Constants.swift 包含一个结构:

struct Constants {
  struct Network {
    #if APITESTS
    static let serverURL = "https://www.servertest.com"
    #else
    static let serverURL = "https://www.server.com"
    #endif
  }
}

在我的测试目标 Other Swift Flags 中,我添加了 -DAPITESTS 然后用CMD+U运行单元测试 在我的测试中出现的日志中,我可以看到这里选择了“https://www.server.com”,而它应该是 servertest。

如果我使用 #if DEBUG 而不是 APITESTS,它就可以工作。 如果我尝试在 Active Compilation Conditions 中添加 APITESTS(就像 DEBUG),它仍然无法识别。

有清楚的解释吗?

最佳答案

当你在测试目标中定义一个条件时,它只会影响测试;主应用程序是基于其通常的配置构建的。我在这里看到的两个简单的解决方案是设置一个环境变量或在主应用程序中定义一个特殊的测试配置,然后设置方案以使用该配置进行测试。

enter image description here

enter image description here

enter image description here

enter image description here

关于swift - Xcode 代码中无法识别的其他 Swift 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45973013/

相关文章:

iOS 从 Swift 类/对象重新加载 UITableView

ios - 更改 View Controller Swift 后的问题

swift - UITableView 复选框(按下另一个按钮时选择一个按钮)

ios - 删除 NSLayoutConstraint 的正确方法

ios - cellForRowAtIndexPath 中的数组索引超出范围

swift - 类型向下转换在 Swift 中找不到原始方法

iphone - 仪器 ObjectAlloc : Explanation of Live Bytes & Overall Bytes

xcode - 在 Xcode 中的当前行下方打开一个新行

ios - 无法在模态 Controller View 前面添加 View

iphone - 使用图片大小 ios 设置 View 框架