Update: Stripping NSLog output for Xcode release builds

I previously posted a solution for stripping NSLog entries from Xcode release builds. However, that method no longer works in Xcode 4, since the variable it relied on (__OPTIMIZE__) is no longer defined by default. There is an alternative now though, since DEBUG is always defined. I’ve copied the bulk of my reasoning from the old post below, and put the new solution at the end, hope this helps…

I’ve had to move my webpage and looking at my incoming site stats it seems one of the most visited pages from my old site was the info on how to easily remove NSLog output from release builds for Xcode so here it is again for those that need it…

Objective-C developers frequently use NSLog to output strings to the console, particularly for debugging purposes. However, when you build a release version you don’t usually want any console output. This is especially true if you are building iPhone applications, where console output is more costly than on desktop OS X and can slow your app down. I’ve seen a few versions of tricks to help you remove NSLog output on release builds (for example, this one), but they all have two problems in common. Firstly, in most you have to edit your project settings and manually define a DEBUG constant. I find this rather annoying, as it just becomes a hassle to remember to go in and edit the project settings every time you start a new application. Secondly, they normally result in defining an alternative output method, such as DLog instead of NSLog, so you have to remember to start using the new version instead, and would have to go back and replace NSLogs in all your old code.

I came up with this small amendment that you put in your app’s prefix header, which I believe is a little neater than the other solutions I’ve seen…

#ifndef DEBUG
#define NSLog(...) {}
#endif

As DEBUG is normally defined for debug builds, you no longer have to go into your project settings and add any new definitions every time you create a new app. Furthermore, by overriding NSLog rather than providing an alternative definition, you can keep using NSLog normally instead of having to use an alternatively named alias. Therefore, I think this is the best solution, as all you have to do is copy and paste the above into your prefix header

  • Pingback: Marek Bell » Stripping NSLog output for Xcode release builds()

  • You can change the project settings for a C or Visual C debug configuration in the Property Pages dialog box, as discussed in How to: Set Debug and Release Configurations . The following tables show where to find debugger-related settings in the Property Pages dialog box.