Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xcode change global variable for each target

I have a series of targets, and each one looks at a global variable I am currently storing in a constants file for the name of the directory in which that app's resources live. However, this means that I have to change this value each time i change the target, which is a terrible process.

What is the best way to change that value in each target? Or should i specify this value somewhere in my target plist ? If so how do i retrieve that value?

Thanks!

like image 716
averydev Avatar asked Sep 13 '10 23:09

averydev


People also ask

How do I change targets in Xcode?

Go to project's settings in the Project Navigator panel. Under the Targets sections, right click the existing target and select Duplicate to copy your existing target. 2. Xcode will ask you if your new target is for iPad development.

How do I set a deployment target in Xcode?

Build Settings The dropdown menu of the Deployment Info section is nothing more than a convenient way to define a build setting. Select the Build Settings tab at the top and search for deployment target. The Deployment section shows four build settings that relate to the deployment target: iOS Deployment Target.

How do you make a local variable global in Swift?

There's another and efficient way to create and store global variable is using a struct, you should always create a struct and encapsulate all the global variable inside it and can use in any class wherever we want., Let's see how we can do it. This is how we can create global variables in swift.

What does target mean in Xcode?

A Target specifies a product to build and contains the instructions for building the product from a set of files in a project or workspace. An Xcode scheme defines a collection of targets to build, a configuration to use when building, and a collection of tests to execute.


1 Answers

With your project open in Xcode, look in the Groups & Files panel for your Target. Right click to bring up your popup menu. Select Get Info. Select the build tab. Scroll down to the "Preprocessor Macros" and add something like "MY_OPTION_1". Do something similar for your other targets. Say "MY_OPTION_2", "MY_OPTION_3", etc.

Now, in your code you can do a define test. Like

#ifdef MY_OPTION_1
   // define values here
#endif

#ifdef MY_OPTION_2
   // define values here
#endif

#ifdef MY_OPTION_3
   // define values here
#endif

Then, depending on what target you use, the values within the corresponding defines are used.

like image 160
No one in particular Avatar answered Oct 16 '22 04:10

No one in particular