A simple method to debug your iPhone application code is to use NSLog() statements at strategic points. I often find myself printing out the response from server calls using NSLog. Even after the iPhone client development is done, these log statements can be useful to debug server issues. But you obviously don’t want to fill the console log in your released code.
To skip lines of code you don’t want to include in the release build you can use a preprocessor instruction:
#ifdef DEBUG NSLog(@"Server Response: %@", response); #endif
Unfortunately Xcode does not automagically define DEBUG when you do a debug build. So you have to add a definition to your project target.
1. Be sure you select Debug in the Configuration drop down.
2. Select Settings Defined at This Level in the second drop down to reduce the number of fields shown.
3. Now click the tool button in the bottom left corner and select Add User-Defined Setting from the menu.
4. Enter the following name value pair:
Now when you build and run in debug configuration you will see the log messages, but those lines of code will not be included in your release builds.
Update: Use GCC_PREPROCESSOR_DEFINITIONS instead of OTHER_CFLAGS. See comments below.
Update 2: See this post for a vastly improved method: The Evolution of a Replacement for NSLog
Update 3: See this post if you’re working with an SDK 3.0 project: How To Add DEBUG Flag In A 3.0 Project In Xcode