Sending zero to a CGPoint type parameter

Suppose I have this method:

- (void)placeView:(UIView*)theView withCenterIn:(CGPoint)centerPoint; 

So, I pass the view and point to the center of the view.

But it happens that I do not need to indicate the center, just an idea.

Passing "nil" results in an error.

Please tell me how to skip the passage of the central point.

Keep in mind that I need to use this method as follows:

 - (void)placeView:(UIView*)theView withCenterIn:(CGPoint)centerPoint{ if(centerPoint == nil){//and I understand that it a wrong comparison, as I cannot pass "nil" to CGPoint //set a random center point } else{ //set that view to the specified point } } 

Thank you in advance

+5
source share
2 answers

CGPoint is a C struct , you cannot pass nil for it. You can create a separate method that does not accept unnecessary CGPoint and get rid of your if , for example:

 - (void)placeView:(UIView*)theView withCenterIn:(CGPoint)centerPoint{ //set that view to the specified point } - (void)placeView:(UIView*)theView { //set a random center point } 

If you insist on keeping one method, you can designate one point as β€œspecial” (say, CGMakePoint(CGFLOAT_MAX, CGFLOAT_MAX) ), wrap it in #define and use nil instead.

Another solution is to wrap your CGPoint in NSValue :

 NSValue *v = [NSValue withPoint:CGMakePoint(12, 34)]; CGPoint p = [v pointValue]; 
+5
source

You cannot use nil as a no-dot indicator because it is for objects only, and CGPoint is a struct . (As dasblinkenlight already said.)

In my geometry library, I defined CGPoint β€œzero” for use as β€œno sense” as well as for validation. Since the components of a CGPoint are CGFloat s, and the float already have an "invalid value" representation - the NAN defined in math.h - I believe that the best way is to use:

 // Get NAN definition #include <math.h> const CGPoint WSSCGPointNull = {(CGFloat)NAN, (CGFloat)NAN}; BOOL WSSCGPointIsNull( CGPoint point ){ return isnan(point.x) && isnan(point.y); } 
+12
source

All Articles