Thursday, March 31, 2011

Is It Possible to NSLog C Structs (Like CGRect or CGPoint)?

I want to be able to debug C structures without having to explicitly type every property that they consist of.

i.e. I want to be able to do something like this:

CGPoint cgPoint = CGPointMake(0,0);
NSLog(@"%@",cgPoint);

Obviously the '%@' won't work, hence the question.

From stackoverflow
  • You can try this:

    NSLog(@"%@", NSStringFromCGPoint(cgPoint));
    

    There are a number of functions that convert the various CG structs into NSStrings. The reason it doesn't work is because %@ signifies an object. A CGPoint is a C struct (and so are CGRects and CGSizes).

  • I use the following macro to help me out with NSRect:

    #define LogRect(RECT) NSLog(@"%s: (%0.0f, %0.0f) %0.0f x %0.0f",
        #RECT, RECT.origin.x, RECT.origin.y, RECT.size.width, RECT.size.height)
    

    You could do something similar for CGPoint:

    @define LogCGPoint(POINT) NSLog(@"%s: (%0.0f, %0.0f)",
        #POINT POINT.x, POINT.y);
    

    Using it as follows:

    LogCGPoint(cgPoint);
    

    Would produce the following:

    cgPoint: (100, 200)
    

0 comments:

Post a Comment