[英]How do I check the TARGET_NAME of my iPhone app on XCode?
I'm trying to have 2 version of my iPhone application within the same XCode project. 我试图在同一个XCode项目中拥有我的iPhone应用程序的2个版本。 The codebase it's almost the same and where I need to have different behaviours I've decided to use preprocessor's conditionals and the
${TARGET_NAME}
tag. 代码库几乎相同,我需要有不同的行为,我决定使用预处理器的条件和
${TARGET_NAME}
标签。
I've set the OTHER_CFLAGS
to contain " -DTARGET_NAME=${TARGET_NAME}
". 我已将
OTHER_CFLAGS
设置为包含“ -DTARGET_NAME=${TARGET_NAME}
”。
Then in my code I tried to do 然后在我的代码中我试着做
#if TARGET_NAME == myApp
NSLog(@"pro");
#elif TARGET_NAME == myAppLite
NSLog(@"lite");
#endif
Unfortunately I always get "lite" printed out since TARGET_NAME == myApp
it's always true: since TARGET_NAME
is defined. 不幸的是,由于
TARGET_NAME == myApp
,我总是打印出“精简版”,因为TARGET_NAME
已定义,所以它始终是真的。 I cannot for the life of me figure out how to evaluate this string comparison. 我不能为我的生活弄清楚如何评估这个字符串比较。 Any idea?
任何的想法?
thanks in advance 提前致谢
You can't compare strings like that in an #if
block. 您无法比较
#if
块中的字符串。 Instead, add the defines to each specific target. 而是将定义添加到每个特定目标。 For instance, on the full version's target, open the Info panel and go to the build tab and add something like
FULL_VERSION
to the GCC_PREPROCESSOR_DEFINITIONS
build setting. 例如,在完整版本的目标上,打开“信息”面板并转到“构建”选项卡,然后将类似
FULL_VERSION
添加到GCC_PREPROCESSOR_DEFINITIONS
构建设置中。 Then, for the lite target, enter something like LITE_VERSION
. 然后,对于lite目标,输入类似
LITE_VERSION
。 In your code, you can do: 在您的代码中,您可以:
#ifdef FULL_VERSION
NSLog(@"Full");
#else
NSLog(@"Lite");
#endif
Actually you can get the target's name to compare it, but this will not skip unnecessary code from other targets at compile time, to do this: 实际上你可以得到目标的名称来比较它,但这不会在编译时从其他目标中跳过不必要的代码,为此:
First go to menu Product -> Scheme -> Edit Scheme... (or CMD + <) Then in the arguments section, add inside environment variables something like: 首先转到菜单Product - > Scheme - > Edit Scheme ...(或CMD + <)然后在arguments部分中添加内部环境变量,例如:
In your code you can get the target's name as: 在您的代码中,您可以获得目标的名称:
NSString *targetName = [[NSProcessInfo processInfo] environment][@"TARGET_NAME"];
NSLog(@"target = %@", targetName); // Will print the target's name
You can compare that string now in runtime. 您可以在运行时比较该字符串。
But following your example: if you want that all the Pro version code to be omitted at compile time. 但是按照你的例子:如果你想在编译时省略所有的Pro版本代码。 You should do what @jason-coco says.
你应该做@ jason-coco说的话。 And go to preprocessor macros in build settings, and add
$(TARGET_NAME)
there: 并转到构建设置中的预处理器宏 ,并在那里添加
$(TARGET_NAME)
:
The code inside the #define will be compiled and executed if my target is "MLBGoldPA" 如果我的目标是“MLBGoldPA”,那么#define中的代码将被编译并执行
#if defined MLBGoldPA
NSLog(@"Compiling MLBGoldPA");
#endif
to get your conditional evaluation working, you have to do something like: 要使您的条件评估有效,您必须执行以下操作:
#define myApp 1
#define myAppLite 2
beforehand, like in your _Prefix.pch file. 事先,就像你的_Prefix.pch文件一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.