Search code examples
objective-cmacrosc-preprocessornslog

How to implement NSLog to trace CGPoint coordinates


I'm trying to find a simple way to call a NSLog to trace myPoint coordinates.
I figured out that I can define a macro like this:

#define NSLogPoint(__P__) NSLog(@"%s x:%f , y:%f", #__P__, __P__.x, __P__.y)

Using # before the parameter it will trace it as is,
so I could have:

 CGPoint myPoint = CGPointMake(3,4);
 NSLogPoint(myPoint);

that will trace:

myPoint x:3.000000 , y:4.000000

is there a better way to do that?


Solution

  • To have no decimal places:

    #define NSLogPoint(__P__) NSLog(@"%s x:%.0f , y:%.0f", #__P__, __P__.x, __P__.y)
    

    To have two decimal places:

    #define NSLogPoint(__P__) NSLog(@"%s x:%.2f , y:%.2f", #__P__, __P__.x, __P__.y)