Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differentiate between two Xcode targets with Swift

Tags:

xcode

ios

swift

How can I differentiate between two Xcode targets with Swift? The idea is to make a free and a paid version of an app with the same code base in Xcode.

With objective C I could use preprocessor macros but with Swift those are unavailable.

like image 627
João Colaço Avatar asked Dec 03 '14 23:12

João Colaço


1 Answers

In Xcode, go into the build configuration for a target. Find the section called Swift Compiler - Custom Flags, which contains a setting called Other Swift Flags.

Add a command-line flag for the compiler to add a flag, pretty much just like you’d do with the C compiler.

Swift Compiler Flags

Now you’ve got -D Something being passed to the Swift compiler. In your Swift code, you can now do this:

#if Something     let foo = "bar" #endif 

It looks a lot like the C preprocessor, but unlike C, all code in all conditional sections has to be syntactically correct or the program won’t compile. So, you can set a flag on each target in the build settings and use them in your code.

like image 145
i40west Avatar answered Oct 09 '22 06:10

i40west