Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Local variables set to nil? (Objective-C)

I'm reading a book on Objective-C and the author said that if local variables aren't assigned a value they will be set to nil, but static variables will be set to zero. So, I set up int a and didn't assign it a value. Then NSLog(@"%i", a) to display it and a was displayed as zero. I was a little confused on that and I was wondering if someone could clarify it for me?

like image 539
stumped Avatar asked Apr 05 '12 02:04

stumped


1 Answers

With ARC enabled, your Objective-C object pointer variables will be set to nil regardless of where you create them.

Without ARC, and for built in C types, your variables will not be initialized.

Instance variables of Objective-C objects are always set to 0 (or nil) when you allocate an object.

Statics are set to 0.

I've gotten in the habit of always giving a default value to variables, though. It's been a good habit to have.

like image 52
wbyoung Avatar answered Sep 28 '22 05:09

wbyoung