Setting up user specific preprocessor macros for Xcode
Asked Answered
P

3

4

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?

Picaroon answered 3/5, 2013 at 1:9 Comment(4)
Could you use an Info.plist instead of #ifdef? Do you really need build differently? If so could you use different build configurations? How many different SOME_VARIABLE's would there be.Teetotum
I'm trying to do something similar to this post: #9088880 but I don't see the value of the macro in code.Picaroon
But are you doing a conditional compile or just need the value in your code. I set the version number minor and major and git hash in my Info.plist with a build script then read the Info.plist in my code to get the values and display them. See my answer second to last in post: #3730686Teetotum
I want to conditionally compile some code based on a specific username.Picaroon
T
5

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

Teetotum answered 4/5, 2013 at 0:15 Comment(1)
I tried that but I'm not getting the value set in the Build Settings, just the name of the define. I see the environment value expanded to the expected value in the Xcode project settings pane but when I tried to print out the define value, I see the name of the variable, not the value.Picaroon
R
2

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.

Reinforce answered 22/5, 2015 at 12:20 Comment(0)
S
2

in swift: add DEBUG_$(USER) to "Active Compilation Conditions"

there is no way to assign a value

Syd answered 5/9, 2017 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.