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

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


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.


All instance variables in an Objective-C class are initialized to zero. So all pointers are nil, numbers are 0, and structs are zeroed. Since the CGRect is a plain struct, it will be initialised to origin.x=0, origin.y=0, size.width=0, size.height=0.

So to test if your CGRect has been set, you need to compare it (by value) to zero. The CGRectIsEmpty function will do exactly this:

if (CGRectIsEmpty(ppGoalFrame)){    // ...}


From CGGeometry.h:

/* Return true if `rect' is empty (that is, if it has zero width or height),   false otherwise. A null rect is defined to be empty. */

an empty rectangle is one that has no area, where on or both sides are zero.
Use instead:

CGRect newRect = CGRectNull;