Local variables set to nil? (Objective-C)

Go To StackoverFlow.com

18

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?

2012-04-05 02:47
by stumped
You should try NSString a - JoshRagem 2012-04-05 02:50
If the book said that as you've written it, I would recommend tossing it in the trash and finding a different book. Nil and 0 are effectively synonymous. Local variables (save for object references under ARC) will be undefined. Static and instance variables will be nil/zero/NULL/Nil, which are synonymous - bbum 2012-04-05 03:45
Agreed with bbum; that's terrible. What the hell is this book? See also: http://stackoverflow.com/questions/9726817/can-variables-be-set-randomly-when-declaring-them-again/9726948#972694 - Josh Caswell 2012-04-05 06:44


46

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.

2012-04-05 03:26
by wbyoung
What about C types with ARC? What about if I have a BOOL when using ARC? Thanks - Ricardo 2014-05-24 11:00
@Ricardo they should be handled according to C rules. Statics will be 0 and all others should be considered garbage - wbyoung 2014-05-28 20:19
Thanks. So, something like @property (nonatomic, assign) BOOL flag; is gargabe, isn't it? because is not staitc. However in my app I see it's 0 always. I don't have these rules very clear - Ricardo 2014-06-02 14:00
@Ricardo the property is backed by an instance variable, not a local variable. So it falls into the instance variable rule and will be set to 0/FALSE/nil - wbyoung 2014-06-03 02:55
For anyone looking for a source to this answer see Apple's document Transitioning to ARC Release Notes. Specifically the section titled Stack Variables Are Initialized with nil.

https://developer.apple.com/library/content/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.htm - palmi 2018-03-01 00:52



12

No2. Just as in "plain" C, local variables are not assigned a default value. (Although you may get lucky the first time part of the stack is used: do not rely on this!.)

Anyway, nil is 01 -- that is, nil == 0 is always true -- so NSLog("@%i", nil) says "hey, log the argument as an integer", which is ... 0.

Happy coding.


1 See nil in gdb is not defined as 0x0? which covers the technical definition, including the Objective-C++ case, in more detail. Note that the type changes depending upon architecture as well so "@%i" could very well be wrong for a particular system.

2 See wbyoung's answer for ARC-specific rules.

2012-04-05 02:56
by NoName
Does the same thing go for static variables? Since, the author said that assigning zero to static variables would be redundant because their default value is zero - stumped 2012-04-05 03:19
With ARC enabled, objects are - wbyoung 2012-04-05 03:28
@stumped: Yes, static variables are intialized to 0 - Josh Caswell 2012-04-05 06:41
Ads