在 Objective-C 中,是否为尚未分配值 nil 的 int 变量? [英] In Objective-C, are int variables that haven't been assigned a value nil?
问题描述
如果我的 .h
文件中有这个:
If I have this in my .h
file:
int index;
然后在 .m
文件中我有:
And then in the .m
file I have:
if (index == nil)
而且我还没有为 index
赋值,这会成真吗?
and I haven't assigned a value to index
, will that come up true?
编辑
我猜 nil 仅用于对象.那么一个没有赋值的 int 是什么状态呢?
EDIT
I guess nil is only used for objects. So what is the state of an int that hasn't been assigned a value?
推荐答案
int 没有nil"这样的东西.那是一个对象值.至于默认初始化为哪些变量:
There is no such thing as "nil" for ints. That's an object value. As for what variables are initialized to by default:
- 非静态局部变量不会被初始化为任何定义的值(实际上,它们通常具有之前在它们占用的内存中的任何位模式)
- 静态变量初始化为0
- 全局变量初始化为 0
- 实例变量初始化为0
请注意,上面的第一条规则也适用于局部对象变量.它们不会为您初始化为零.(不过,具有对象类型的实例变量将是.)
Note that the first rule above also applies to local object variables. They will not be nil-initialized for you. (Instance variables with object types will be, though.)
这篇关于在 Objective-C 中,是否为尚未分配值 nil 的 int 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!