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.
For variables of class types and other reference types, this default value is null . However, since structs are value types that cannot be null , the default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null .
You can use regular C structs all you want. Your example tries to put references to an Objective-C object, NSString , into a struct , which is incompatible with ARC. Structs are typically used for simple data structures.
Que: Can we initialize structure members within structure definition? No! We cannot initialize a structure members with its declaration, consider the given code (that is incorrect and compiler generates error).
Pointers have no default value. The value they have is just whatever junk was in the memory they're using now. Sometimes a specific compiler will zero out memory, but that's not standard so don't count on it.)
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)) { // ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With