发送nil到CGPoint类型参数 [英] Sending nil to CGPoint type parameter

查看:80
本文介绍了发送nil到CGPoint类型参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有此方法:

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

所以我传递了视图和一个指向视图中心的点.

So I pass the view and a point to te the view's center.

但是碰巧我不需要指定中心,只需指定视图即可.

But it happens that I do not need to specify the center, just the view.

传递"nil"会导致错误.

Passing "nil" leads to error.

请建议如何跳过通过中心点.

Please, suggest how to skip passing the center point.

请记住,我需要使用这种方法:

Keep in mind that I need to use that method like this:

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

预先感谢

推荐答案

CGPoint是C struct,您不能为此传递nil.您可以创建一个不使用不必要的CGPoint的单独方法,并摆脱if语句,如下所示:

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
}

如果坚持使用一种方法,则可以将一个点指定为特殊"(例如,CGMakePoint(CGFLOAT_MAX, CGFLOAT_MAX)),将其包装在#define中,然后使用代替nil.

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.

另一种解决方案是将CGPoint包裹在NSValue中:

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

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

这篇关于发送nil到CGPoint类型参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆