views:

1308

answers:

4

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.

A: 

I don't think you can do a binary comparison of a C structure, which is what CGRect is. A quick Google search and checking the C For Dummies Desk Reference didn't turn anything up. You could declare CGRect ppGoalFrame, initialize the members of the structure with default values (if they're not already), and test the structure members.

Update Edit: This question has been asked for C structures.

C.D. Reimer
The story is different for Objective-C when discussing instance variables.
gavinb
@gavinb: Maybe. I'm still brushing up on my C before I plunge into Objective-C.
C.D. Reimer
+9  A: 

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.

Andrew

Andrew
Ended up using CGRectIsEmpty to test. Don't have to set them in any way. Your answer got me on the right track though, so thanks.
Typeoneerror
+3  A: 

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. There is a helper function to compare two CGRect instances, and also a constant struct predefined for a zeroed rect, CGRectZero. You can test it like this:

if (CGRectEqualToRect(ppGoalFrame, CGRectZero))
{
    // ...
}
gavinb
+1  A: 

There is no universal "nothing" value. There's nil for objects and NULL for pointers, but there's no equivalent for plain value types — they always have some value, even if it's just nonsense. You can take an arbitrary value and say "I'm going to call this the null value for this type" (say, #define IntNull 4444444 and hope some calculation never accidentally returns that value), but there's no real null value.

In the case of CGRect in particular, Apple used the "pick an arbitrary value" tactic and defined CGRectNull, but CGRect variables aren't set to that by default — you have to specifically set it yourself. By default, CGRect instance variables are set to CGRectZero, but remember that this is just a normal CGRect struct at location (0,0) with 0 width and 0 height. And local CGRects in a function have a totally unpredictable value.

Chuck