The Ultimate Print Statement

Some of the most useful functions of the Foundation classes in Cocoa that are not often well known are the functions that convert from non-objects to NSString.  Vincent Gable extended code from Dave Dribin's blog post to create a function that prints an expression and its evaluation, no matter the type. This macro is a must-have for your Objective-C/C++ debugging toolkit, and replaces all print statements.

#define echo(_X_) do{\
	__typeof__(_X_) _Y_ = (_X_);\
	const char * _TYPE_CODE_ = @encode(__typeof__(_X_));\
	NSString *_STR_ = VTPG_DDToStringFromTypeAndValue(_TYPE_CODE_, &_Y_);\
		MBLog(@"%s = %@", #_X_, _STR_);\
		MBLog(@"Unknown _TYPE_CODE_: %s for expression %s in function %s, file %s, line %d", _TYPE_CODE_, #_X_, __func__, __FILE__, __LINE__);\