Sending nil to CGPoint type parameter

后端 未结 2 560
小蘑菇
小蘑菇 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:23

    You can't use nil as a "no point" indicator, because it is only for objects, and CGPoint is a struct. (As dasblinkenlight has already said.)

    In my geometry library, I've defined a "null" CGPoint to use as a "no point" placeholder, and a function to test for it. Since the components of a CGPoint are CGFloats, and floats have a "invalid value" representation already -- NAN, defined in math.h -- I think that's the best thing to use:

    // Get NAN definition
    #include 
    
    const CGPoint WSSCGPointNull = {(CGFloat)NAN, (CGFloat)NAN};
    
    BOOL WSSCGPointIsNull( CGPoint point ){
        return isnan(point.x) && isnan(point.y);
    }
    

提交回复
热议问题