.xcconfig?如何设置环境变量 [英] .xcconfig? How to set environment variables

查看:189
本文介绍了.xcconfig?如何设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Xcode的新手.

I'm new to Xcode.

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

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.

我正在使用Swift + Xcode 6,但是我认为这与Xcode 5相同.

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

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

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

例如,我创建了一个Environment.xcconfig文件.我用

For example, I created a Environment.xcconfig file. I populated it with

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

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

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

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

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

这非常令人沮丧.有什么想法吗?

This is extremely frustrating. Any ideas?

推荐答案

  1. 如果您具有Pod配置,请设置配置层次结构:
  1. Setup config hierarchy in case you have pods configs:

或者,如果您根本没有任何Pod,则配置"层次结构将如下所示:

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

  1. 为每个配置文件创建所有键值对(在这种情况下,有3个用于dev/adhoc/appstore构建的配置文件).每个配置文件都有相同的键集:

  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:

将每个密钥添加到生成器:

Add each key to the generator:

然后仅使用代码中的键:

Then just use the keys in your code :

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

Swift 2.2的更新:不再起作用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆