在Objective-C中,没有为int变量赋值nil吗? [英] In Objective-C, are int variables that haven't been assigned a value nil?

查看:96
本文介绍了在Objective-C中,没有为int变量赋值nil吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的.h文件中有此文件:

If I have this in my .h file:

int index;

然后在.m文件中,我有:

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中,没有为int变量赋值nil吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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