Wednesday, April 13, 2011

Better Debug for CGRect, CGPoint and CGSize

Can't remember how many times I've wanted to debug what is the size of a frame. Can't remember how many times I've written x and y coordinates, followed by width and height, to console log.

All wasted time and effort, while I could have been doing something more productive! Live and learn: just found out that iOS SDK contains helper routines for that task:
// Hard
//NSLog(@"%.0fx%.0f (%.0fx%.0f)",
//    frame.origin.x, frame.origin.y,
//    frame.size.width, frame.size.height);
// Easy
NSLog(@"My box: %@",
    NSStringFromCGRect(frame));
Check the iOS SDK documentation for UIKit String Conversions, there's more useful stuff (for example NSStringFromCGPoint and NSStringFromCGSize).

3 comments: