开发者

Auto-initialization of object variables

开发者 https://www.devze.com 2023-02-15 05:27 出处:网络
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 Program

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号