Default value of an Objective-C struct and how to test

typeoneerror picture typeoneerror · Mar 10, 2010 · Viewed 28k times · Source

I'm trying to test if a property has been set yet. I know that with objects that I've got:

CGRect ppGoalFrame;
LocalPlaySetup *localPlaySetup;

and I can test

if (localPlaySetup == nil)

but if I attempt to test the CGRect with == nil or == NULL

if (ppGoalFrame == nil)

I get

invalid operands to binary == (have 'CGRect' and 'void *')

So is the CGRect "void", null, nil...? before it's set? Obviously I can't compare CGrect to a void pointer (I can't use ppGoalFrame == void); is there another way to test this? The Objective-C so far is pretty easy to understand but as soon as the C looms, I get a bit lost.

Answer

Andrew picture Andrew · Mar 10, 2010

Only pointers can be null. CGRect is a struct - it represents a contiguous block of memory. The only way to tell if it has been set it to check its contents.

Apple does provide a constant CGRectNull. You could set your variable to this and use the CGRectIsNull function to determine if it has been set. CGRectNull is not the same as CGRectZero so you need not worry if the desired value is zero.

Note that CGRectNull simply contains a CGRect struct filled with values that Apple can later identify for the CGRectIsNull function. It is not the same null as when comparing pointers.