Print CGRect And CGPoint Using NSLog

October 9, 2012

CGRect and CGPoint are both defined as C structures:

struct CGPoint {
   CGFloat x;
   CGFloat y;
};
typedef struct CGPoint CGPoint;
 
struct CGRect {
   CGPoint origin;
   CGSize size;
};
typedef struct CGRect CGRect;

When debugging, it can be helpful to print the values of these structures. There are two methods in the UIKit that make this easy, converting the structures to NSString objects:

CGRect rect = [[UIScreen mainScreen] applicationFrame];
NSLog(@"rect: %@", NSStringFromCGRect(rect));
 
CGPoint point = CGPointMake(100.00, 200.0);
NSLog(@"point: %@", NSStringFromCGPoint(point));

The output will look as follows:

rect: {{0, 20}, {320, 460}}
point: {100, 200}