Conditional exclusion of code in Swift

Fabrizio Bartolomucci picture Fabrizio Bartolomucci · Jul 14, 2015 · Viewed 7.4k times · Source

I am trying to exclude some parts of a Swift file for a specific target. Yet I did not find any replacement of the #ifndef objective-c directive and moreover if I use a form of the kind:

#if taxi_coops
func pippo(){
    println("pippo");
}
#else
func triggerActiveLocationUpdate(entering:Bool){}
#endif

The preprocessor totally ignores the directive and tries to compile triggerActiveLocationUpdate. Please note the #if taxi_coops directive is respected in other parts of the same file.

Is there some way to simply exclude some pieces of code in Swift and/or why my fix does not work?

Answer

par picture par · Oct 15, 2016

In Swift, use #if ! as a replacement for #ifndef, e.g.:

#if !os(OSX)
    // compiled when not on OS X
#endif