.xcconfig? How to set environment variables

I am new to Xcode.

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

I use Swift + Xcode 6, but I think this is the same process as Xcode 5.

I have looked through a lot of articles and I think I should use .xcconfig, but this is very obscure.

For example, I created an Environment.xcconfig file. I filled it

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

Then I went to Project -> Info and installed the Debug configuration file on Environment .

Then I tried to access the variable in the code through ${API_BASE_URL} , but I got Use of unresolved identifier 'API_BASE_URL' .

This is extremely frustrating. Any ideas?

+4
source share
2 answers
  • Setting configuration configuration in case you have container configurations: enter image description here

OR, if you do not have any containers, the Configurations hierarchy will look like this: enter image description here

  1. Create all the key-value pairs for each configuration file (in this case there are 3 configuration files for the dev / adhoc / appstore build). Each configuration file has the 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

PS: keys generated in this way are also recognized in .swift files (make sure your Swift project has a bridge header, although you are not using obj-c sources and it will be empty).

UPDATE for Swift 2.2: Swift 2.2 no longer works : GCC_PREPROCESSOR_DEFINITIONS constants are no longer imported

+7
source

You do not want .xcconfig ; which stores settings for Xcode. Instead, you need a property list file ( .plist ). To do this, press command-N to 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 to your application:

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

You can then access your properties through the plist dictionary, like any other dictionary value. Please note that this is not a Swift Dictionary , since it does not have a constructor that takes the path to the file to load.

Updated 10/21/2015: Now this Swift 2.0 answer is compatible. pathForResource() now returns optional.

0
source

Source: https://habr.com/ru/post/1245927/


All Articles