问题
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?
回答1:
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
回答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 =1
in 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 #if
wouldn't. Of course, you might define it to be any value you wish, depending on what you want.
回答3:
in swift: add DEBUG_$(USER) to "Active Compilation Conditions"
there is no way to assign a value
来源:https://stackoverflow.com/questions/16350109/setting-up-user-specific-preprocessor-macros-for-xcode