Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a CGPoint has been initialised?

Tags:

objective-c

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.

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;

}

like image 994
Alan Avatar asked Jan 19 '12 15:01

Alan


4 Answers

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;
    
like image 116
mipadi Avatar answered Oct 14 '22 06:10

mipadi


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

if (!CGPointEqualToPoint(_graphOrigin, CGPointZero)) {  

}
like image 40
François Marceau Avatar answered Oct 14 '22 08:10

François Marceau


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

like image 11
kennytm Avatar answered Oct 14 '22 06:10

kennytm


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]; 
like image 5
Zigglzworth Avatar answered Oct 14 '22 08:10

Zigglzworth