Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up user specific preprocessor macros for Xcode

I'd like to be able to have specific code blocks such as #ifdef SOME_VARIABLE and the value of variable would be filled at project's build time if the project is being built on a specific user's machine.

Is that possible?

like image 559
Paul Hansen Avatar asked Jan 13 '23 11:01

Paul Hansen


2 Answers

You set the value in the "Preprocessor Macros" Build Settings. Setting "SOME_VARIABLE=${USER}" in the build settings is equivalent to #define SOME_VARIABLE "jappleseed" in your code.

Then in your code you can do this:

#define jappleseed 1
#define sjobs      2

#if DEV_USER == jappleseed
    NSLog(@"Hi Jhonny");
#elif DEV_USER == sjobs
    NSLog(@"Hi Steve");
#endif

Note: This is a contrived example if you really want the string "jappleseed" for use in your code you should be using an Info.plist and not #define

like image 200
GayleDDS Avatar answered Jan 21 '23 05:01

GayleDDS


In Xcode 6 (at least) you can include your username as part of the name of the macro in the preprocessor macros section of the Build Settings:

ENV_USER_$(USER)=1 (this will define the macro ENV_USER_myusername)

Then, in your code, you can use

#if ENV_USER_myusername
...
#endif

The =1in the definition may be redundant, since Xcode seems to define the variable as true by default. We put it there just to be on the safe side and be able to use either #if or #ifdef. This way there is no concern that it might be defined, but evaluating to false, in which case #ifdef would still work but #ifwouldn't. Of course, you might define it to be any value you wish, depending on what you want.

like image 20
entonio Avatar answered Jan 21 '23 06:01

entonio