xcode .xcconfig?如何设置环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24668284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 05:11:54  来源:igfitidea点击:

.xcconfig? How to set environment variables

iosxcodeswift

提问by Dean

I'm new to Xcode.

我是 Xcode 的新手。

I spent the last two days trying to figure out how to test my app on my iPhone which accesses a web service. On the simulator I can use a hard-coded 'localhost' variable, but I don't want to hardcode all the configuration settings.

在过去的两天里,我试图弄清楚如何在访问网络服务的 iPhone 上测试我的应用程序。在模拟器上,我可以使用硬编码的“localhost”变量,但我不想对所有配置设置进行硬编码。

I'm using Swift + Xcode 6 but I think this is the same process as Xcode 5.

我正在使用 Swift + Xcode 6,但我认为这与 Xcode 5 的过程相同。

I looked through lots of articles and I think I'm supposed to use .xcconfig, but it's very unclear.

我浏览了很多文章,我认为我应该使用 .xcconfig,但它很不清楚。

For example, I created a Environment.xcconfigfile. I populated it with

例如,我创建了一个Environment.xcconfig文件。我填充了它

API_BASE_URL = "http://localhost:4000/api/v1"

I then went into Project -> Info and set the Debug configuration file to Environment.

然后我进入 Project -> Info 并将调试配置文件设置为Environment.

I then tried to access the variable in code via ${API_BASE_URL}but I get Use of unresolved identifier 'API_BASE_URL'.

然后我尝试通过代码访问变量,${API_BASE_URL}但我得到Use of unresolved identifier 'API_BASE_URL'.

This is extremely frustrating. Any ideas?

这是非常令人沮丧的。有任何想法吗?

回答by kas-kad

  1. Setup config hierarchy in case you have pods configs: enter image description here
  1. 如果您有 pods 配置,请设置配置层次结构: 在此处输入图片说明

OR if you haven't any pods at all, the Configurations hierarchy gonna look like this: enter image description here

或者,如果您根本没有任何 Pod,则配置层次结构将如下所示: 在此处输入图片说明

  1. Create all the key-value pairs for each config file (in this case there are 3 config files for dev/adhoc/appstore builds). Each config file has same set of keys: enter image description here

  2. Add each key to the generator: enter image description here

  3. Then just use the keys in your code : enter image description here

  1. 为每个配置文件创建所有键值对(在本例中,有 3 个用于 dev/adhoc/appstore 构建的配置文件)。每个配置文件都有相同的一组键: 在此处输入图片说明

  2. 将每个键添加到生成器: 在此处输入图片说明

  3. 然后只需使用代码中的键: 在此处输入图片说明

PS: the keys generated this way are also recognizable in .swift files (make sure you have a bridging header in your Swift project, even though you don't use obj-c sources and it will be empty).

PS:以这种方式生成的键也可以在 .swift 文件中识别(确保你的 Swift 项目中有一个桥接头,即使你不使用 obj-c 源并且它会是空的)。

UPDATE for Swift 2.2: doesn't work anymoreSwift 2.2: GCC_PREPROCESSOR_DEFINITIONS constants no longer imported

Swift 2.2 更新:不再工作Swift 2.2:GCC_PREPROCESSOR_DEFINITIONS 常量不再导入

回答by NRitH

You don't want .xcconfig; that stores settings for Xcode. Instead, you want a property list (.plist) file. To do so, hit command-Nto create a new file, then select iOS > Resources > Property List. Use the plist editor to add keys, values, and value types. Then load your properties by adding these lines somewhere in your app:

你不想要.xcconfig; 存储 Xcode 的设置。相反,您需要一个属性列表 ( .plist) 文件。为此,请点击command-N创建一个新文件,然后选择iOS > Resources > Property List。使用 plist 编辑器添加键、值和值类型。然后通过在您的应用程序中的某处添加这些行来加载您的属性:

if let filePath = NSBundle.mainBundle().pathForResource("Latin Conjugations", ofType:"plist") {
    let plist = NSDictionary(contentsOfFile:filePath)
}

You can then access your properties via the plist dictionary like you would any other dictionary value. Note that it's not a Swift Dictionary, since that doesn't have a constructor that takes a file path to load.

然后,您可以像访问任何其他字典值一样通过 plist 字典访问您的属性。请注意,它不是 Swift Dictionary,因为它没有接受文件路径加载的构造函数。

Updated 10/21/2015:This answer is now Swift 2.0-compliant. pathForResource()now returns an Optional.

2015 年 10 月 21 日更新:此答案现在符合 Swift 2.0。pathForResource()现在返回一个可选。