Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is wrong when checking if the objects is nil

I have read the docs from apple http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithObjects/WorkingwithObjects.html

It says that we can check if object is null like...

 XYZPerson *somePerson;
    // somePerson is automatically set to nil
 if (somePerson != nil) {
        // somePerson points to an object
    }

Most probably i am doing something wrong but need your help to find what it is. I have a class C2,I create the C2 object but not initialising,when checking with the code below ,app writes NOT NİL to output.What am i doing wrong.

Thanks for your help.

C2 * o3;

    if (o3 != nil) {
        NSLog(@"NOT NİLLLL");
    }else{
        NSLog(@"NOT");
    }
like image 754
user1902563 Avatar asked Dec 17 '25 14:12

user1902563


1 Answers

Unlike members of classes that are zeroed out on initialization, local variables are not initialized automatically by pre-ARC compilers. If you would like your local o3 to be nil, you need to initialize it yourself:

C2 * o3 = nil;

The behavior of not initializing locals unless explicitly directed by the program comes from C: there, too, the locals are not initialized by default.

like image 118
Sergey Kalinichenko Avatar answered Dec 20 '25 07:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!