Friday, August 29, 2014

A Macro to Dump CGRect

Recently, I worked on a class derived from UIScrollView.  In order to dump the CGRect's conveniently, I mean better than NSStringFromCGRect(), I introduced the following macro:

#ifdef DEBUG
    #define DBG(format, ...)                NSLog(@"%d,%s,"format,__LINE__,__FUNCTION__,##__VA_ARGS__);
    #define DUMP_RECT(rect)                 DBG("\n"                   \
                                                #rect"\n"              \
                                                "....x:|%.0f|\n"       \
                                                "....y:|%.0f|\n"       \
                                                "....width:|%.0f|\n"   \
                                                "....height:|%.0f|\n", \
                                                rect.origin.x,         \
                                                rect.origin.y,         \
                                                rect.size.width,       \
                                                rect.size.height)
#else
    #define DBG(format, ...)
    #define DUMP_RECT(rect)
#endif

Note the stringizing operator (#) operator to dump the "rect". Its sample use in LPTSCrollView class:

- (void)reset
{
...
    DUMP_RECT(self.frame)
    DUMP_RECT(self.bounds)
...
}

Its sample outputs:

2014-08-29 16:15:03.829 PhotonPro[18253:60b] 98,-[LPTScrollView reset],
self.frame
....x:|0|
....y:|0|
....width:|320|
....height:|568|
2014-08-29 16:15:03.830 PhotonPro[18253:60b] 100,-[LPTScrollView reset],
self.bounds
....x:|0|
....y:|0|
....width:|320|
....height:|568|

Hopefully, it will help somebody!