iphone: get User Defined variable in Target's setting by code?

柔情痞子 提交于 2019-12-02 19:06:28

As the Info.plist file is preprocessed too, you can use this approach:

Define a User defined setting in your build settings, for Example CLASS_NAME. And a key to your Info.plist-file. Name the key CLASS_NAME and set the value to ${CLASS_NAME}.

You can then access this setting by:

NSString* className = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CLASS_NAME"];

You can not directly use a variable defined in the build settings. These variables are meant to be used by build tools.

Instead define a preprocessor macro in the Preprocessor Macros Variable like 'MYVAR=5'. You can access these macros in your code like:

#if MYVAR==5
    //Do something
#endif

Please note, that the evaluation of these expressions happens at build-time not at runtime.

It is very typical use to just define a Macro without caring for the value. For example define 'DEBUG=1' in the debug build settings and 'RELEASE=1' in the release build settings.

You can then test using #ifdef or #ifndef

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