【问题标题】:How to add a precompiled header in xcode 11如何在 xcode 11 中添加预编译头文件
【发布时间】:2020-06-18 03:26:36
【问题描述】:
我想在 pre-compiled header 中为我的应用程序的不同目标定义几个自定义值。我尝试了以下方法来定义标题,但没有一个起作用:
- 通过添加来自
Editor -> Add Build Setting -> Add User Defined setting 的标头并分配键值对。
- 在
Build Settings 中将Preprocess info.plist 文件设置为YES,并在Info.plist Preprocessor Prefix file 中设置头文件(.h)。
如何添加和设置 pch 密钥,以便使用 #if 为 UAT 目标设置 true 和为生产目标设置 false?
【问题讨论】:
标签:
ios
swift
xcode11.3
pch
【解决方案1】:
如果唯一目的是为不同的环境设置不同的值 - development, QA, UAT, Production,则可以使用 xcconfig 文件。
不需要条件语句,但根据配置,它将采用适当的值。
所涉及的步骤是:
添加不同的架构和不同的构建配置。
通过选择文件->新建->文件和"Configuration Settings file"添加配置设置文件->Development.xcconfig, UAT.xcconfig, Production.xcconfig等
-
在配置文件中添加您选择的自定义键,并在键值对中添加它们的相关值:
endpointUrl = "Production_Url" 用于 Production.xcconfig
endpointUrl = "UAT_Url" 用于 UAT.xcconfig
- 添加相关的 plist 文件并将 .xcconfig 文件中的密钥添加到 .plist 文件中。为 UAT.xcconfig 文件创建 UAT.plist 并为其他支持的配置创建类似的方式。
- 为
Project -> Info -> Configurations下的每个构建配置设置适当的plist路径
- 创建一个 .swift 文件来读取配置文件如:
公共枚举 PlistKey {
案例端点URL
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"
}
}
}
- 调用url为:
let server_url = Environment().configuration(PlistKey.EndpointURL)
更详细的版本在:https://www.freecodecamp.org/news/managing-different-environments-and-configurations-for-ios-projects-7970327dd9c9/
【解决方案2】:
如果您使用的是 swift。然后您可以添加自定义 swift 标志。在 Build Settings -> Swift Compiler - Custom Flags 下。
然后在代码中使用
#if YourFlag
....
#else