Logging Arbitrary Types

Here’s something handy for your debug logging:

The Most Useful Objective-C Code I’ve Ever Written

LOG_EXPR(x) is a macro that prints out x, no matter what type x is, without having to worry about format-strings (and related crashes from eg. printing a C-string the same way as an NSString). It works on Mac OS X and iOS…

And it works on scalar values, not just Objective-C objects like the “%@” argument sorts for you. Pretty nifty, huh? Download it from github here if so. However, it is limited to a fixed set of types; and as further noted here:

Almost elegant cave man debugging

…  this could be avoided by parsing the @encode() string rather than recognising specific strings. As it happens, I had a partially working parser. It is now wholly working and added a convenience macro similar to Vicent’s, and you can find it here. It handles most structs, unions and arrays automatically…

So yes, if you like to see really detailed structure information in your log messages, there you go!

h/t: @ricardoquesada!

Alex | September 18, 2010

Leave a Reply