Sending nil to CGPoint type parameter

后端 未结 2 550
小蘑菇
小蘑菇 2021-01-01 02:23

Suppose I have this method:

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

So I pass the view and a point to te the

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-01 02:39

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

    - (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 could designate one point as "special" (say, CGMakePoint(CGFLOAT_MAX, CGFLOAT_MAX)), wrap it in a #define, and use instead of nil.

    Yet another solution would be to wrap your CGPoint in NSValue:

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

提交回复
热议问题