Collection of Objective-C Debugging Macros

In a previous post, Yet Another Debug Output Replacement, I wrote a macro that I prefer over NSLog as the output does not prepend the date/time and object information as does NSLog.

For example, the debug macro in the earlier post shows output as shown in the final line below, which (in my opinion) is much easier to read over the line that displays the default date and time information:

I thought I would pass along a few more macros that I use on a regular basis for printing debug information on rectangles, points and sizes. I’ve also included a means to turn debug information on/off so debug statements are not included in builds targeted as final releases.

// Debug
#define DEBUG_ON  // Toggle to DEBUG_OFF to hide all debug code 
#ifdef DEBUG_ON
  #define debug(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
  #define debug(format, ...) 
#define debugRect(rect) debug(@"%s x:%.4f, y:%.4f, w:%.4f, h%.4f", #rect,
    rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)
#define debugSize(size) debug(@"%s w:%.4f, h:%.4f", #size, size.width, size.height)
#define debugPoint(point) debug(@"%s x:%.4f, y:%.4f", #pt, pt.x, pt.y)

Additional Reading

  • Apple has an excellent Technical Note on iOS Debugging Magic. Tips range from working with the CrashReporter to debugging push notifications, Objective-C and much more.
  1. Why not to use NSStringFromCGPoint, NSStringFromCGRect, NSStringFromCGSize, etc.?

    • I’ve got more flexibility in how I display the information when I use the macro as it is defined.

Comments are closed.