Auto-initialization of object variables
In Objective C on iOS, are instance variables of object reference type (id
, N开发者_StackOverflowSObject *
, etc.) auto-initialized to nil upon object allocation or not?
Yes, see The Objective C Programming Language documentation on Apple's site.
The alloc and allocWithZone: methods initialize a newly allocated object’s isa instance variable so that it points to the object’s class (the class object). All other instance variables are set to 0. Usually, an object needs to be more specifically initialized before it can be safely used.
ivars are, but local variables aren't. See this question
Apple's NSObject class reference doc says:
"The isa instance variable of the new instance is initialized to a data structure that describes the class; memory for all other instance variables is set to 0."
0 is the same as nil for a pointer.
精彩评论