How to Use NSLog to Debug CGRect and CGPoint

CGPoint and CGRect are structures (versus objects) and therefore the old NSLog standby %@ will not work as expected.

Here is how each structure is defined:

If you attempt NSLog to use the traditional ‘print object’ notation such as this:

the compiler will generate a warning: Format specifies type ‘id’ but the argument has type ‘CGPoint’ (aka ‘struct CGPoint’)

Good news is, this is easy to fix:

The output of converting a CGPoint to an NSString looks as follows:

Output CGRect using NSLog

Likewise, CGRect is a structure and will also give NSLog troubles. The solution is similar:

And the output:

CGRect, CFDictionaryRef and NSLog

When working with CGRect structures, there is another solution, you can convert the rect to a CFDictionaryRef and print as shown below:

Which returns:

If for some reason you need to keep the dictionary object around, here is how you can so if you are using ARC:

The code on line 5 moves the CGRef to and Objective-C object, and also hands the memory management over to ARC.