How to conditionally compile the objective-C code with minimum change of the code base?

Here I try to share some tips about how to avoid write tons of #ifdef #end to pollute the code, just to avoid compiling some legacy code into a project that may not need them.

First, in the code where it uses the objects that you want to exclude this time, you have to use #ifdef to cover the code. For example, make the code section in between:

#ifdef ABC

….

#endif

Then whether the code inside is complied or not, you can control it by going to Build Settings, then searching preprocessor and make sure “ABC=1” are not in the DEBUG and RELEASE section. This way ABC is not defined, so the code section won’t compile.

However, if you use this way to hide certain class implementation, unfortunately, doing this still cannot avoid compilation of the class, In this case you need to also exclude the specific files from compilation. This can be done by adding user-defined settings. Clicking the ‘+’ icon in the build settings of the project to add “EXCLUDED_SOURCE_FILE_NAMES” and the value should be all the files you want to exclude from compiling, separated by space.