xcode iphone:通过代码在Target的设置中获取用户定义的变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3316293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iphone: get User Defined variable in Target's setting by code?
提问by KONG
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.
我的项目有多个目标。每个目标都有自己的Class
用于设置内容的文件。我想将该Class
名称存储在目标设置(Info.plist
或目标的建筑物设置)中。这样我就可以根据这个设置定义我需要在每个目标中使用哪个类。
According to this question, I put "a target-specific User Defined
variable" in each Target's Building Setting.
根据这个问题,我User Defined
在每个目标的建筑设置中放置了“特定于目标的变量”。
But don't know how to get it back in my code?
但不知道如何在我的代码中取回它?
回答by tonklon
As the Info.plist file is preprocessed too, you can use this approach:
由于 Info.plist 文件也经过预处理,您可以使用这种方法:
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}
.
在构建设置中定义用户定义的设置,例如 CLASS_NAME。以及 Info.plist 文件的密钥。将键命名为 CLASS_NAME 并将值设置为${CLASS_NAME}
。
You can then access this setting by:
然后,您可以通过以下方式访问此设置:
NSString* className = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"CLASS_NAME"];
回答by tonklon
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:
而是在预处理器宏变量中定义一个预处理器宏,如“MYVAR=5”。您可以在代码中访问这些宏,例如:
#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.
仅定义宏而不关心值是非常典型的用法。例如,在调试构建设置中定义“DEBUG=1”,在发布构建设置中定义“RELEASE=1”。
You can then test using #ifdef or #ifndef
然后您可以使用 #ifdef 或 #ifndef 进行测试
#ifdef DEBUG
// Log
#endif