views:

107

answers:

2

Hi, I have a method like this:

- (CGPoint) _convertCGPointCT2UIKit:(CGPoint)ctPoint{
 CGPoint uikitPoint = CGPointMake(ctPoint.x + INITIAL_HORIZ_OFFSET, 
          self.bounds.size.height - ctPoint.y - INITIAL_VERT_OFFSET);
 return uikitPoint;
}

Is there any way I can make this a macro? I tried this but I get errors like "; expected before )" and so on.

#define CGPointUIKit2CT(p) CGPointMake(p.x - INITIAL_HORIZ_OFFSET, self.bounds.size.height - p.y - INITIAL_VERT_OFFSET);

thanks in advance.

Ignacio

+3  A: 

A couple of rules of thumb to guide you:

  • Wrap everything in parentheses
  • Don't end a macro with a semicolon, that's probably what's generating your errors. Instead put the semicolon in your code when you use it.

Here's my answer:

 #define CGPointUIKit2CT(p) CGPointMake(((p).x) - INITIAL_HORIZ_OFFSET, self.bounds.size.height - ((p).y) - INITIAL_VERT_OFFSET)
Chris Garrett
Ouch! what a silly mistake. a colon!Thanks Chris
nacho4d
@nacho: If this answer solved your problem, you should check the green checkmark to mark it as the accepted answer. That gives Chris some bonus rep and it increases your acceptance rate -- something that will make others more eager to answer your questions in the future.
Matt B.
+1  A: 

While there may be reasons to avoid method dispatch for something like this, there is no reason to do it as a macro instead of a static inline function:

static inline
CGPoint CGPointUIKit2CT(UIView *self, CGPoint  ctPoint) {
  CGPoint uikitPoint = CGPointMake(ctPoint.x + INITIAL_HORIZ_OFFSET,
      self.bounds.size.height - ctPoint.y - INITIAL_VERT_OFFSET);'

  return uikitPoint;
}

That will compile out to the same thing as a macro, but will provide better debugging and profiling information.

Louis Gerbarg