Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an object was initialized? Objective-C

Tags:

objective-c

I'm wondering if there is a way to check wether a variable that has been declared has been assigned or initialized or not in Objective C..

thank you

like image 701
cromestant Avatar asked Dec 16 '10 13:12

cromestant


2 Answers

All instances variables are set to 0 (or nil for objects) in the alloc method, see The Objective-C Programming Language.

This means you can check the variable with

if (!var) {...}

Be aware though that there is no way to differentiate between the state after initialization or just being set to 0.

like image 180
w-m Avatar answered Nov 15 '22 05:11

w-m


Check if the object is 'nil':

if(object == nil){ };
like image 27
Mikael Avatar answered Nov 15 '22 05:11

Mikael