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

2

32

My project have multi-targets. Each target has its own Class file for setting stuff. I want to store that Class name in a target setting (Info.plist or Target's Building setting). So that I can define which class I need to use in each target based on this setting.

According to this question, I put "a target-specific User Defined variable" in each Target's Building Setting.

But don't know how to get it back in my code?

Tunable answered 23/7, 2010 at 7:54 Comment(2)
Which target setting do you mean? The build settings? The Info.plist? Where did you set the variable?Melamie
I put it in the Target's Build Setting.Tunable
M
69

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"];
Melamie answered 23/7, 2010 at 9:49 Comment(0)
M
10

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
Melamie answered 23/7, 2010 at 8:35 Comment(3)
Oh, I see. How about the Info.plist. Can I store the Class name there, and get it from code?Tunable
I have added another answer, for howto use the info.plist.Melamie
If I understand well, there's no way to retrieve the build settings value directly? Because for secrets and keys (that change per configuration) it would be better to not pass through the Plist.Anthropo

© 2022 - 2024 — McMap. All rights reserved.