Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 510
typeoneerror Avatar asked Mar 10 '10 05:03

typeoneerror


People also ask

What is the default value of a struct in C?

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 .

Can we use struct in Objective-C?

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.

Can you initialize a value in a struct?

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).

What is a pointers default value?

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.)


2 Answers

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.

like image 196
Andrew Avatar answered Oct 05 '22 02:10

Andrew


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)) {     // ... } 
like image 25
gavinb Avatar answered Oct 05 '22 01:10

gavinb