如何在 XCode 上检查我的 iPhone 应用程序的 TARGET_NAME?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1027091/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:37:37  来源:igfitidea点击:

How do I check the TARGET_NAME of my iPhone app on XCode?

iphonexcodepreprocessor

提问by onigiri

I'm trying to have 2 version of my iPhone application within the same XCode project. 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.

我正在尝试在同一个 XCode 项目中拥有 2 个版本的 iPhone 应用程序。代码库几乎相同,我决定使用预处理器的条件和${TARGET_NAME}标记在哪些地方需要有不同的行为。

I've set the OTHER_CFLAGSto 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 == myAppit's always true: since TARGET_NAMEis defined. I cannot for the life of me figure out how to evaluate this string comparison. Any idea?

不幸的是,我总是打印出“lite”,因为TARGET_NAME == myApp它总是正确的:因为TARGET_NAME已定义。我一生都无法弄清楚如何评估这个字符串比较。任何的想法?

thanks in advance

提前致谢

回答by Jason Coco

You can't compare strings like that in an #ifblock. 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_VERSIONto the GCC_PREPROCESSOR_DEFINITIONSbuild setting. Then, for the lite target, enter something like LITE_VERSION. In your code, you can do:

你不能在#if块中比较这样的字符串。相反,将定义添加到每个特定目标。例如,在完整版本的目标上,打开“信息”面板并转到“构建”选项卡并FULL_VERSIONGCC_PREPROCESSOR_DEFINITIONS构建设置中添加类似内容。然后,对于 lite 目标,输入类似LITE_VERSION. 在您的代码中,您可以执行以下操作:

#ifdef FULL_VERSION
NSLog(@"Full");
#else
NSLog(@"Lite");
#endif

回答by LightMan

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 + <)然后在参数部分,添加内部环境变量,例如:

setup environment variables

设置环境变量

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. You should do what @jason-coco says. And go to preprocessor macrosin build settings, and add $(TARGET_NAME)there:

但是按照您的示例:如果您希望在编译时省略所有 Pro 版本代码。你应该按照@jason-coco 所说的去做。并转到构建设置中的预处理器宏,并在$(TARGET_NAME)那里添加:

enter image description here

在此处输入图片说明

The code inside the #define will be compiled and executed if my target is "MLBGoldPA"

如果我的目标是“MLBGoldPA”,#define 中的代码将被编译和执行

#if defined MLBGoldPA
    NSLog(@"Compiling MLBGoldPA");
#endif

回答by LightMan

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 文件中一样。