DEV Community

Raheel
Raheel

Posted on

Why isn't my Preprocessor Macro working?

We have a hybrid Objective-C/Swift app. All of the new classes are written in Swift, but we still update the Objective-C code for bug fixes and feature enhancements. In our app, we define Preprocessor Macros and use it for build configuration specific logic e.g. #if DEBUG.

Recently, we had to add support an enterprise build in our app. An enterprise build is a build where the IPA is stored on the company's internal secure network and served from there. I added a new configuration, "Enterprise" to our app. I added the macro under "Apple Clang - Preprocessing" --> "PreProcessor Macros"
alt text

However, after making this change and adding the logic to our Swift code nothing happened. I spent hours trying to see if there was any issue with our logic. Was there any typos in the variable name? Had I set the macro in the wrong configuration? Was it a cache issue? The answer to all these questions were no. It turns out that the Swift compiler doesn't include a preprocessor. It takes advantage of compile-time attributes to accomplish the same functionality. We must define them under "Swift Compiler - Custom Flags" --> "Active Compilation Conditions".
alt text

After adding the the custom flags, my code worked as expected. One thing to keep in mind is that if you have both Objective-C and Swift classes relying on a macro, you will need to add it to both places.

Top comments (0)