1

Have my own macro defined in myProject.pch file,

For example:

#define Enable_Analytics

And i want to enclose few statements of code at multiple places inside

#ifdef Enable_Analytics
  // Code statements which has to executed only if Enable_Analytics is defined
#endif

This is useful to include/remove code based on the macro. In Objective-C this works but in swift i get error. How to use #ifdef in swift?

abelenky
  • 63,815
  • 23
  • 109
  • 159
Kishoretheju
  • 827
  • 3
  • 8
  • 18
  • Did you have a look at [#ifdef replacement in the Swift language](http://stackoverflow.com/questions/24003291/ifdef-replacement-in-the-swift-language) ? – Martin R May 09 '17 at 13:33
  • @MartinR, yes i have looked at that. I don't want to move that to build settings because I have so many macros like that in my project. If there is no other solution then i will think about it. – Kishoretheju May 09 '17 at 13:48

1 Answers1

0

You can call the C preprocessor on just about and kind of file even if it is not C or a C like language. It is usually available as cpp on most Unix like systems. Just run cpp your-file.swift and the C preprocessor should preprocess the file just as it would C.

However I do not know of a way to make the preprocessor use defines within a project file. So you probably will have to manually specify them on the command line like -DMACRO_NAME=somevalue.

Alternatively you could #include a file containing the defines within each of your swift files and the preprocessor will insert them.

Vality
  • 6,577
  • 3
  • 27
  • 48
  • @MartinR neither does C. The #include directive is part of the C preprocessor. A completely separate and standalone language. The #include will be processed by the preprocessor in the same way as #defines are. – Vality May 09 '17 at 14:12