Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding pre-processor macros in Xcode 10

Tags:

xcode

ios

swift

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:

enter image description here

then, under my app's second target (my Today Extension), I set IS_MAIN=0.

enter image description here

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.

like image 896
jjjjjjjj Avatar asked Jun 17 '19 04:06

jjjjjjjj


3 Answers

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

enter image description here

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

like image 198
kerry Avatar answered Oct 09 '22 07:10

kerry


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. enter image description here

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!

enter image description here

like image 41
Vinay Hosamane Avatar answered Oct 09 '22 06:10

Vinay Hosamane


Add under Active Compilation Conditions as,

enter image description here

like image 21
Kamran Avatar answered Oct 09 '22 07:10

Kamran