2

from the reference How to handle multiple targets in XCode using swift language? and https://www.appcoda.com/using-xcode-targets/

I have created three targets, each target having different bundle ids and app icons. I have also added different flag to the "Other swift flags" - > Custom swift flag section

like

for first target I added "-DGOLD" , for second target I added "-DSILVER" and for third target I added "-DPLATINUM".

In the AppDelegate I wrote a code

#if GOLD
print ("Gold")
#elseif SILVER
print ("Silver")
#else
print ("Platinum")
#endif

FYI, I am using Xcode 8.3

I tried to run the first target, I always getting "Platinum". Please provide me how to get the flags correctly

Sridhar
  • 2,108
  • 10
  • 42
  • 79

2 Answers2

7

For Xcode 8 and above, use Active Compilation Conditions setting in Build settings / Swift compiler - Custom flags.

This is the new build setting for passing conditional compilation flags to the Swift compiler. Simply add flags like this: GOLD, SILVER etc. without -D etc. Then check it with compilation conditions like this:

#if GOLD
//
#elseif SILVER
//
#else
//
#endif
Sagar D
  • 3,016
  • 1
  • 15
  • 32
1

I am working with Xcode 9. I am having same problem and looked for an answer for a few hours and finally found the solution. At each Target, Build Settings -> Other Swift Flags, I added the wanted flag like this:

Target1 -> -DTARGET1

Target2 -> -DTARGET2

Then in my code used:

#if TARGET1
//
#elseif TARGET2
//
#endif
Techno Mag
  • 21
  • 4