Setting up user specific preprocessor macros for Xcode

假装没事ソ 提交于 2020-01-04 03:25:08

问题


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 =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.




回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!