0

我想为pre-compiled header我的应用程序的不同目标定义几个自定义值。我尝试了以下方法来定义标题,但没有一个起作用:

  1. 通过添加标头Editor -> Add Build Setting -> Add User Defined setting并分配键值对。
  2. Preprocess info.plist文件设置为 YES inBuild Settings并将头文件 (.h) 设置为Info.plist Preprocessor Prefix file.

如何添加和设置 pch 密钥,以便我可以使用 #iftrue为 UAT 目标和false生产目标设置?

4

2 回答 2

1

如果唯一的目的是为不同的环境设置不同的值 - development, QA, UAT, Productionxcconfig可以使用文件。

不需要条件语句,但根据配置,它将采用适当的值。

所涉及的步骤是:

  1. 添加不同的架构和不同的构建配置。

  2. 通过选择 File->New->File and "Configuration Settings file"-> Development.xcconfig, UAT.xcconfig, Production.xcconfigetc添加配置设置文件

在此处输入图像描述

  1. 在配置文件中添加您选择的自定义键,并在键值对中添加它们的相关值:

    endpointUrl = "Production_Url"用于生产.xcconfig

    endpointUrl = "UAT_Url"对于 UAT.xcconfig

在此处输入图像描述

  1. 添加相关的 plist 文件并将 .xcconfig 文件中的密钥添加到 .plist 文件中。为 UAT.xcconfig 文件创建 UAT.plist 并为其他支持的配置创建类似的方式。

在此处输入图像描述

  1. 在下为每个构建配置设置适当的 plist 路径Project -> Info -> Configurations

在此处输入图像描述

  1. 创建一个 .swift 文件来读取配置文件,例如:

公共枚举 PlistKey { case EndpointURL

func value() -> String {
    switch self {
    case .EndpointURL:
        return "endpointUrl"
    }
} }

公共结构环境{

    fileprivate var infoDict: [String: Any]  {
        get {
            if let dict = Bundle.main.infoDictionary {
                return dict
            }else {
                fatalError("Plist file not found")
            }
        }
    }
    public func configuration(_ key: PlistKey) -> String {
        switch key {
        case .EndpointURL:
            return infoDict[PlistKey.EndpointURL.value()] as? String ?? "noValue"
        }
    }
}
  1. 将网址称为:

let server_url = Environment().configuration(PlistKey.EndpointURL)

更详细的版本在:https ://www.freecodecamp.org/news/managing-different-environments-and-configurations-for-ios-projects-7970327dd9c9/

于 2020-06-18T07:49:40.297 回答
0

如果你使用的是 swift。然后您可以添加自定义 swift 标志。在 Build Settings -> Swift Compiler - Custom Flags 下。

然后在代码中使用

#if YourFlag
....
#else
于 2020-06-18T05:37:35.297 回答