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;
}
A
CGPointis 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 unsetCGPoint, but that’s also a valid coordinate. Or you could use negativexandyvalues 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:
CGPoint. This value can be set toNULLwhen uninitialized. Of course, you have to worry aboutmallocing andfreeing the value.Store the
CGPointalongside aBOOLcalledpointInitializedor somesuch, initially set toNO, but set toYESonce the point has been initialized. You can even wrap that up in a struct: