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

前端 未结 4 768
-上瘾入骨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:44

    nil is a specific value reserved for Objective-C object pointers. It is very similar to NULL (0) but has different semantics when used with message passing.

    local primitive data types like int's are not initialized in Objective-C. The value of index will not be defined until you write to it.

    void foo () {
       //initialize local variable
       int x = 5;
    }
    
    //initialise static variable
    static int var = 6; 
    

提交回复
热议问题