局部变量设置为nil? (目标C) [英] Local variables set to nil? (Objective-C)

查看:87
本文介绍了局部变量设置为nil? (目标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.

这篇关于局部变量设置为nil? (目标C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆