13

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?

Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75
  • Possible duplicate of [In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project](http://stackoverflow.com/questions/24111854/in-absence-of-preprocessor-macros-is-there-a-way-to-define-practical-scheme-spe). – Martin R Jul 14 '15 at 15:04
  • Btw, you have now asked quite a few questions and got answers to many of them. Please don't forget to *accept* answers that helped. – Martin R Jul 14 '15 at 15:09
  • 1
    It may be a duplicate, as I said in other paces it worked, at last the if part. But in my code it does not work. Also I did not find anywhere how to express #ifndef. – Fabrizio Bartolomucci Jul 14 '15 at 15:36
  • Most questions I answered myself, and took care to report my findings. For the others I hope to cover all of them. – Fabrizio Bartolomucci Jul 14 '15 at 15:37
  • You can also accept your own answers to mark the problem as solved. – Martin R Jul 14 '15 at 15:39
  • Sure, but in this case I have no answer myself, nor from other users. – Fabrizio Bartolomucci Jul 14 '15 at 15:44

2 Answers2

20

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

#if !os(OSX)
    // compiled when not on OS X
#endif
par
  • 17,361
  • 4
  • 65
  • 80
13

The issue was about replicating the configuration in the Other Swift Flags file in the form

-D option

Apparently Swift ignores the flags in the preprocessor field. Now it accepts ||, && and ! without any problem with syntax:

#if option || !option2
......
#elseif option3
......
#endif
Fabrizio Bartolomucci
  • 4,948
  • 8
  • 43
  • 75