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:
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.
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.
There are two ways to achieve what you want to do. Read this article to understand more about pre processors and swift.
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
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.
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
You need to set swift custom flags to access the pre-macro processors in swift.
If you don't want to set preprocessor macro for multiple architectures, then you can just use like this.
For example here, I have GEO=1 in my debug scheme with value 1.
To access in Objective C there is no problem, but to access in swift you need to expose it in the swift other flags like this.
In your case, you need to add -DIS_MAIN in the custom flag.
I hope it helps!
Add under Active Compilation Conditions
as,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With