局部变量设置为零?(目标-C) [英] Local variables set to nil? (Objective-C)
问题描述
我正在读一本关于 Objective-C 的书,作者说如果局部变量没有赋值,它们将被设置为 nil,但静态变量将被设置为零.所以,我设置了 int a
并且没有给它赋值.然后 NSLog(@"%i", a)
显示它并且 a
显示为零.我对此有点困惑,我想知道是否有人可以为我澄清一下?
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?
推荐答案
启用 ARC 后,无论您在何处创建它们,Objective-C 对象指针变量都将设置为 nil.
With ARC enabled, your Objective-C object pointer variables will be set to nil regardless of where you create them.
如果没有 ARC,对于内置的 C 类型,您的变量将不会被初始化.
Without ARC, and for built in C types, your variables will not be initialized.
在分配对象时,Objective-C 对象的实例变量始终设置为 0(或 nil).
Instance variables of Objective-C objects are always set to 0 (or nil) when you allocate an object.
静态设置为 0.
不过,我已经养成了总是给变量一个默认值的习惯.这是一个好习惯.
I've gotten in the habit of always giving a default value to variables, though. It's been a good habit to have.
这篇关于局部变量设置为零?(目标-C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!