In Objective-C, are int variables that haven't been assigned a value nil?

前端 未结 4 765
-上瘾入骨i
-上瘾入骨i 2021-01-22 12:09

If I have this in my .h file:

int index;

And then in the .m file I have:

if (index == nil)

4条回答
  •  盖世英雄少女心
    2021-01-22 12:43

    There is no such thing as "nil" for ints. That's an object value. As for what variables are initialized to by default:

    • Non-static local variables are not initialized to any defined value (in practice, they will usually have whatever bit pattern was previously in the memory they're occupying)
    • Static variables are initialized to 0
    • Global variables are initialized to 0
    • Instance variables are initialized to 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.)

提交回复
热议问题