[英].xcconfig? How to set environment variables
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. 我花了两天时间试图弄清楚如何在我的iPhone上测试我的应用程序来访问网络服务。 On the simulator I can use a hard-coded 'localhost' variable, but I don't want to hardcode all the configuration settings.
在模拟器上,我可以使用硬编码的“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.xcconfig
file. 例如,我创建了一个
Environment.xcconfig
文件。 I populated it with 我填充它
API_BASE_URL = "http://localhost:4000/api/v1"
I then went into Project -> Info and set the Debug configuration file to Environment
. 然后我进入Project - > Info并将Debug配置文件设置为
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?
有任何想法吗?
OR if you haven't any pods at all, the Configurations hierarchy gonna look like this: 或者,如果您根本没有任何pod,Configurations层次结构将如下所示:
Create all the key-value pairs for each config file (in this case there are 3 config files for dev/adhoc/appstore builds). 为每个配置文件创建所有键值对(在这种情况下,dev / adhoc / appstore构建有3个配置文件)。 Each config file has same set of keys:
每个配置文件都有相同的密钥集:
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 anymore Swift 2.2: GCC_PREPROCESSOR_DEFINITIONS constants no longer imported Swift 2.2的UPDATE:不再起作用 Swift 2.2:GCC_PREPROCESSOR_DEFINITIONS常量不再导入
You don't want .xcconfig
; 你不想要
.xcconfig
; that stores settings for Xcode. 存储Xcode的设置。 Instead, you want a property list (
.plist
) file. 相反,您需要一个属性列表(
.plist
)文件。 To do so, hit command-N
to create a new file, then select iOS > Resources > Property List
. 要执行此操作,请按
command-N
以创建新文件,然后选择iOS > Resources > Property List
。 Use the plist editor to add keys, values, and value types. 使用plist编辑器添加键,值和值类型。 Then load your properties by adding these lines somewhere in your app:
然后通过在应用中的某处添加这些行来加载您的属性:
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. 然后,您可以像任何其他字典值一样通过plist字典访问您的属性。 Note that it's not a Swift
Dictionary
, since that doesn't have a constructor that takes a file path to load. 请注意,它不是Swift
Dictionary
,因为它没有构造函数来加载文件路径。
Updated 10/21/2015: This answer is now Swift 2.0-compliant. 2015年10月21日更新:此答案现在符合Swift 2.0标准。
pathForResource()
now returns an Optional. pathForResource()
现在返回一个Optional。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.