在Xcode 10中添加预处理器宏 [英] Adding pre-processor macros in Xcode 10
问题描述
我正在尝试将PR处理器宏添加到我的Xcode项目中.这个项目共享Swift代码和Objective-C代码,因此我不能使用Swift Compiler -> Other Swift Flags
,因为那样我就无法从Objective-c代码中读取它们.
I am trying to add pr-processor macros to my Xcode project. This project shares both Swift and Objective-C code, so I cannot use Swift Compiler -> Other Swift Flags
, because then I cannot read them from objective-c code.
我的应用程序的主要目标和项目中都设置了以下内容:
I have the following set in both my app's main target and the project:
然后,在我应用的第二个目标(即今日扩展)下,我将IS_MAIN = 0设置为
then, under my app's second target (my Today Extension), I set IS_MAIN=0.
但是,当我运行这段代码时:
However, when I run this code:
#if IS_MAIN
print("main: true")
#else
print("main: false")
#endif
它永远不会打印出正确的照片.
It never prints the correct one.
如何添加此预处理器标志,以便我的Swift文件和Objective-c文件都尊重它?现在,这两种类型都不起作用.
How can I add this preprocessor flag so that it is respected by both my Swift and objective-c files? Right now it works in neither type.
推荐答案
有两种方法可以实现您想做的事情.阅读此文章,以了解有关预处理器和Swift的更多信息.
There are two ways to achieve what you want to do. Read this article to understand more about pre processors and swift.
基本上,在Xcode 8之前,您必须在Build settings
的Other Swift Flags
中添加所需的标志.像这样添加它.请注意-D"选项
Basically, before Xcode 8, you would have to add the required flag in Other Swift Flags
in Build settings
. Add it like this. Note the "-D" option
在Xcode 8之后,Apple添加了另一个名为Active Compilation Conditions
的设置,您可以在其中添加预处理器宏,而无需使用"-D"选项,而swift可以识别它.
After Xcode 8, Apple has added another setting called Active Compilation Conditions
where you can add your pre processor macro without the "-D" option and swift will be able to recognize it.
在两种情况下,都不会添加值
=1
.您只需在一种配置中提及该标志,而在另一种配置中不提及该标志,您的#if
条件将起作用
In both the cases, you do not add the value
=1
. You just mention the flag in one configuration and do not mention the flag in another and your#if
condition will work
这篇关于在Xcode 10中添加预处理器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!