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

时间:2022-04-21 20:55:45

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_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. I cannot for the life of me figure out how to evaluate this string comparison. Any idea?

不幸的是,由于TARGET_NAME == myApp,我总是打印出“精简版”,因为TARGET_NAME已经定义,所以它始终是真的。我不能为我的生活弄清楚如何评估这个字符串比较。任何想法?

thanks in advance

提前致谢

3 个解决方案

#1


11  

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

您无法比较#if块中的字符串。而是将定义添加到每个特定目标。例如,在完整版本的目标上,打开“信息”面板并转到“构建”选项卡,并将类似FULL_VERSION的内容添加到GCC_PREPROCESSOR_DEFINITIONS构建设置中。然后,对于lite目标,输入类似LITE_VERSION的内容。在您的代码中,您可以:

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

#2


1  

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部分中添加内部环境变量,例如:

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

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 macros in build settings, and add $(TARGET_NAME) there:

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

如何在XCode上查看我的iPhone应用程序的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

#3


-1  

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

#1


11  

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

您无法比较#if块中的字符串。而是将定义添加到每个特定目标。例如,在完整版本的目标上,打开“信息”面板并转到“构建”选项卡,并将类似FULL_VERSION的内容添加到GCC_PREPROCESSOR_DEFINITIONS构建设置中。然后,对于lite目标,输入类似LITE_VERSION的内容。在您的代码中,您可以:

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

#2


1  

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部分中添加内部环境变量,例如:

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

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 macros in build settings, and add $(TARGET_NAME) there:

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

如何在XCode上查看我的iPhone应用程序的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

#3


-1  

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