How do I check if a CGPoint has been initialised?

后端 未结 6 1826
你的背包
你的背包 2021-02-07 02:59

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 initi

相关标签:
6条回答
  • 2021-02-07 03:16

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

    if (!CGPointEqualToPoint(_graphOrigin, CGPointZero)) {  
    
    }
    
    0 讨论(0)
  • 2021-02-07 03:16

    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.

    @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.

    0 讨论(0)
  • 2021-02-07 03:22

    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:

    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.

    To get the point later simply use:

    CGPoint point = [pointVal CGPointValue]; 
    
    0 讨论(0)
  • 2021-02-07 03:26

    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.

    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. 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:

      struct {
          CGPoint point;
          BOOL initialized;
      } pointData;
      
    0 讨论(0)
  • 2021-02-07 03:26

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

    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.

    (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))

    0 讨论(0)
  • 2021-02-07 03:35
    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
    
    0 讨论(0)
提交回复
热议问题