如何检查CGPoint是否已初始化?

时间:2022-11-22 07:33:06

I would like to initially set a CGPoint property to a particular point (middle of screen). Other methods may subsequently wish to change this property. My thoughts were to initialise it if empty in the getter, but I get the message invalid argument type 'struct CGPoint' to unary expression. I also tried using if property == nil or 0 but no joy.

我想首先将CGPoint属性设置为一个特定的点(屏幕中间)。其他方法可能随后希望更改此属性。我的想法是,如果在getter中为空,就初始化它,但我将消息无效的参数类型‘struct CGPoint’设为一元表达式。我也尝试使用if属性= nil或0,但没有乐趣。

Any thoughts?

任何想法吗?

-(CGPoint)graphOrigin
{
    // initialise to centre of screen if has not been set
    if(!_graphOrigin) // this expression is causing the problem
    {
        CGPoint origin = CGPointMake(self.bounds.origin.x + self.bounds.size.width / 2, self.bounds.origin.y + self.bounds.size.height / 2);

        _graphOrigin = origin;
    }

return _graphOrigin;

}

}

6 个解决方案

#1


29  

A CGPoint is a struct, so you can't set it to nil or NULL (it's not a pointer). In a sense, there's really no "uninitialized" state. Perhaps you could use {0.0, 0.0} to designate an unset CGPoint, but that's also a valid coordinate. Or you could use negative x and y values to flag an "uninitialized" point, since negative values can't be valid drawing points, but that's a bit of a hack, too.

CGPoint是一个结构体,所以不能将它设置为nil或NULL(它不是指针)。在某种意义上,实际上没有“未初始化”状态。也许您可以使用{0.0,0.0}来指定一个未设置的CGPoint,但这也是一个有效的坐标。或者您可以使用- x和y值标记一个“未初始化”的点,因为负值不能是有效的绘图点,但这也有点麻烦。

Probably your best bet is to do one of two things:

也许你最好的选择是做以下两件事之一:

  1. Store the property as a pointer to a CGPoint. This value can be set to NULL when uninitialized. Of course, you have to worry about mallocing and freeing the value.
  2. 将属性存储为指向CGPoint的指针。在未初始化时,可以将该值设置为NULL。当然,您必须担心错误定位和释放值。
  3. Store the CGPoint alongside a BOOL called pointInitialized or somesuch, initially set to NO, but set to YES once the point has been initialized. You can even wrap that up in a struct:

    将CGPoint存储在一个名为pointInitialized或somesuch的BOOL旁边,最初设置为NO,但在初始化点之后设置为YES。你甚至可以用结构图来概括:

    struct {
        CGPoint point;
        BOOL initialized;
    } pointData;
    

#2


21  

An easier way would be to initialize _graphOrigin to CGRectZero and change your if statement for this:

一个更简单的方法是将_graphOrigin初始化为cgrect0,并为此更改if语句:

if (!CGPointEqualToPoint(_graphOrigin, CGPointZero)) {  

}

#3


10  

CGPoint does not have an uninitialized state. However, if you consider the point (0, 0) as uninitialized, you could use

CGPoint没有未初始化的状态。但是,如果您认为(0,0)点未初始化,您可以使用

if (_graphOrigin.x == 0 && _graphOrigin.y == 0)
{
    ...

This works because when an Objective-C instance is initialized, all its ivar are cleared to bits of zero, which in the CGFloat representation is 0.0.

这之所以有效,是因为当一个Objective-C实例初始化时,它的所有ivar都被清除为0位,在CGFloat表示形式中是0。0。

(Note: The == is fine here even if the operands are CGFloat because we want to compare with the an exact bit pattern (ignoring the issue of -0))

(注意:==在这里没有问题,即使操作数是CGFloat,因为我们想要与精确的位模式进行比较(忽略-0的问题))

#4


5  

Since CGPointZero (0,0) and any other value you give a point may exist in your context you may want to initialize an NSValue with your point using:

由于CGPointZero(0,0)和您提供的任何其他值可能存在于您的上下文中,您可能希望使用您的点初始化NSValue:

NSValue *pointVal = [NSValue valueWithCGPoint:point];

You could do this based on some condition and then later test the NSValue for nil. NSValue can also be added to an array which would allow you to have an array of points should you need.

你可以基于某种条件来做这个,然后再为nil测试NSValue。NSValue还可以添加到一个数组中,该数组允许您在需要时拥有一个点数组。

To get the point later simply use:

要获得要点,只需使用:

CGPoint point = [pointVal CGPointValue]; 

#5


0  

Create two CGPoint properties, that way they are both "uninitialized". Set one of them and use the second one to check whether or not they are equal.

创建两个CGPoint属性,这样它们都是“未初始化的”。设置其中一个,然后用第二个来检查它们是否相等。

@interface ClassName ()

@property (nonatomic) CGPoint point1;
@property (nonatomic) CGPoint point2;

@end

@implementation ClassName

self.point1 = CGPointMake(69.0f, 180.0f);  //arbitrary numbers

  //if not equal, then if statement proceeds
if (!CGPointEqualToPoint(self.point1, self.point2) {
    //your code here
}

@end

Idk if you'd consider this way hackish though. And I know your question was already answered, but I had kinda the same dilemma till I thought of this.

如果你这样想的话,那就去乡下吧。我知道你的问题已经得到了回答,但在我想到这个之前,我也有过同样的困惑。

#6


0  

static CGPoint kInvalidPoint = {.x = NSIntegerMax, .y = NSIntegerMax};

@implementation MyClass

- init()
{
    self = [super init];
    if (self) {
        _oldPoint = kInvalidPoint;
    }
    return self;
}

- (void)foo
{
    if (CGPointEqualToPoint(self.oldPoint, kInvalidPoint)) {
        // Invalid point.
        return;
    }
}

@end

#1


29  

A CGPoint is a struct, so you can't set it to nil or NULL (it's not a pointer). In a sense, there's really no "uninitialized" state. Perhaps you could use {0.0, 0.0} to designate an unset CGPoint, but that's also a valid coordinate. Or you could use negative x and y values to flag an "uninitialized" point, since negative values can't be valid drawing points, but that's a bit of a hack, too.

CGPoint是一个结构体,所以不能将它设置为nil或NULL(它不是指针)。在某种意义上,实际上没有“未初始化”状态。也许您可以使用{0.0,0.0}来指定一个未设置的CGPoint,但这也是一个有效的坐标。或者您可以使用- x和y值标记一个“未初始化”的点,因为负值不能是有效的绘图点,但这也有点麻烦。

Probably your best bet is to do one of two things:

也许你最好的选择是做以下两件事之一:

  1. Store the property as a pointer to a CGPoint. This value can be set to NULL when uninitialized. Of course, you have to worry about mallocing and freeing the value.
  2. 将属性存储为指向CGPoint的指针。在未初始化时,可以将该值设置为NULL。当然,您必须担心错误定位和释放值。
  3. Store the CGPoint alongside a BOOL called pointInitialized or somesuch, initially set to NO, but set to YES once the point has been initialized. You can even wrap that up in a struct:

    将CGPoint存储在一个名为pointInitialized或somesuch的BOOL旁边,最初设置为NO,但在初始化点之后设置为YES。你甚至可以用结构图来概括:

    struct {
        CGPoint point;
        BOOL initialized;
    } pointData;
    

#2


21  

An easier way would be to initialize _graphOrigin to CGRectZero and change your if statement for this:

一个更简单的方法是将_graphOrigin初始化为cgrect0,并为此更改if语句:

if (!CGPointEqualToPoint(_graphOrigin, CGPointZero)) {  

}

#3


10  

CGPoint does not have an uninitialized state. However, if you consider the point (0, 0) as uninitialized, you could use

CGPoint没有未初始化的状态。但是,如果您认为(0,0)点未初始化,您可以使用

if (_graphOrigin.x == 0 && _graphOrigin.y == 0)
{
    ...

This works because when an Objective-C instance is initialized, all its ivar are cleared to bits of zero, which in the CGFloat representation is 0.0.

这之所以有效,是因为当一个Objective-C实例初始化时,它的所有ivar都被清除为0位,在CGFloat表示形式中是0。0。

(Note: The == is fine here even if the operands are CGFloat because we want to compare with the an exact bit pattern (ignoring the issue of -0))

(注意:==在这里没有问题,即使操作数是CGFloat,因为我们想要与精确的位模式进行比较(忽略-0的问题))

#4


5  

Since CGPointZero (0,0) and any other value you give a point may exist in your context you may want to initialize an NSValue with your point using:

由于CGPointZero(0,0)和您提供的任何其他值可能存在于您的上下文中,您可能希望使用您的点初始化NSValue:

NSValue *pointVal = [NSValue valueWithCGPoint:point];

You could do this based on some condition and then later test the NSValue for nil. NSValue can also be added to an array which would allow you to have an array of points should you need.

你可以基于某种条件来做这个,然后再为nil测试NSValue。NSValue还可以添加到一个数组中,该数组允许您在需要时拥有一个点数组。

To get the point later simply use:

要获得要点,只需使用:

CGPoint point = [pointVal CGPointValue]; 

#5


0  

Create two CGPoint properties, that way they are both "uninitialized". Set one of them and use the second one to check whether or not they are equal.

创建两个CGPoint属性,这样它们都是“未初始化的”。设置其中一个,然后用第二个来检查它们是否相等。

@interface ClassName ()

@property (nonatomic) CGPoint point1;
@property (nonatomic) CGPoint point2;

@end

@implementation ClassName

self.point1 = CGPointMake(69.0f, 180.0f);  //arbitrary numbers

  //if not equal, then if statement proceeds
if (!CGPointEqualToPoint(self.point1, self.point2) {
    //your code here
}

@end

Idk if you'd consider this way hackish though. And I know your question was already answered, but I had kinda the same dilemma till I thought of this.

如果你这样想的话,那就去乡下吧。我知道你的问题已经得到了回答,但在我想到这个之前,我也有过同样的困惑。

#6


0  

static CGPoint kInvalidPoint = {.x = NSIntegerMax, .y = NSIntegerMax};

@implementation MyClass

- init()
{
    self = [super init];
    if (self) {
        _oldPoint = kInvalidPoint;
    }
    return self;
}

- (void)foo
{
    if (CGPointEqualToPoint(self.oldPoint, kInvalidPoint)) {
        // Invalid point.
        return;
    }
}

@end