How to Make the CocoaLumberjack work in your XCode project?

CocoaLumberjack is a good debugging tools that clearly define the log information output level so filtering the log information is possible to achieve. To use it should be very simple due to the Pods package manager tools. Here we introduce a way to let this good tool replace your old NSLog functions.

  1. Make a file named “Podfile” in the root directory of your project. Git it if you wish. Name need to be as indicated.
  2. Go to the directory in the terminal, call “pod install” to let the amazing pods handle the package installation.
  3.  From now on you should have another folder named “Pods” in your project which maintains the 3rd party library for you. For your git, remember to avoid version control on this directory since you have the Podfile so you can install them on other machines. This can avoid version control on the large package which is not necessary at all.
  4. Now it should be OK, but to use CocoaLumberjack, you need to import “CocoaLumberjack.h” in every file you want to have log, which is tedious. However, in XCode you can define a prefixed header which will be processed for the entire project.
  5. Go to the “Supporting Files” directory in your project’s left panel,  Add file there by clicking right mouth button. Choose others in the dialog and choose PCH file.
  6. Inside the created PCH file, added #import “CocoaLumberjack.h” there and it is done.
  7. You also need to make sure the compiler uses the PCH file. This can be done by going to Build Settings and search “Prefix Header”, go to the Language section and add Prefix Header to point to your file. Don’t forget to Enable Precompile Prefix Header also.

Now you should be able to log your project easier.

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.