Replacement for the #ifndef directive

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?

Answered by fbartolom in 29214022

The problem actually boiled down to entering the settings as -D option also in the Swift options. Apparently Swift does not read the C, C++ options. After that it understood #if option, #if option && option and #if !option like a charm.

a. There's no replacement in Swift for #ifdef or #ifndef, only for #if


b. For a test of the form #if MyDef or #if !MyDef, you have to define MyDef in your build settings, and the definition that works for me is "MyDef=1" in Preprocessor Macros.


c. There is no preprocessor in Swift.


d. When you use #if in Swift, the code that is excluded is still syntax checked and must be well-formed. This is a consequence of (c).


In the example you gave, the alternatives do look well-formed. Are you sure that the #else case was really being compiled, or was it perhaps just being syntax-checked?

The problem actually boiled dow to entering the settings as -S option also in the Swift option. Apprently Swift does not read the C, C++ options. After that it understood #if option, #if option && option and #if !option like a charm.

Accepted Answer

The problem actually boiled down to entering the settings as -D option also in the Swift options. Apparently Swift does not read the C, C++ options. After that it understood #if option, #if option && option and #if !option like a charm.

Replacement for the #ifndef directive
 
 
Q