A great way to improve NSLog
NSLog has always been my weapon of choice when I needed to debug in my early days of iPhone development. Recently I came across a very interesting post from Vincent Gable which has created a macro called LOG_EXPR(x) 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.
Here is the macro:
#define LOG_EXPR(_X_) do{\
__typeof__(_X_) _Y_ = (_X_);\
const char * _TYPE_CODE_ = @encode(__typeof__(_X_));\
NSString *_STR_ = VTPG_DDToStringFromTypeAndValue(_TYPE_CODE_, &_Y_);\
if(_STR_)\
NSLog(@"%s = %@", #_X_, _STR_);\
else\
NSLog(@"Unknown _TYPE_CODE_: %s for expression %s in function %s, file %s, line %d", _TYPE_CODE_, #_X_, __func__, __FILE__, __LINE__);\
}while(0)
Download VTPG_Common.m and VTPG_Common.h from Vincent Gable’s github repository, and add them to your Xcode project.
Now just add the line #import “VTPG_Common.h” to your prefix file (named
#ifdef __OBJC__
#import
// maybe other files, depending on project template...
#import "VTPG_Common.h"
#endif
Now LOG_EXPR() will work everywhere in your project.