Does Objective-C guarantee the initialization of interface member data?
In an interface such as this:
@interface M开发者_Go百科yClass : NSObject
{
bool PlainOldBool;
}
@end
... does PlainOldBool
get auto-initialized to false, or is it necessary to use an init method to do that explicitly?
Yes (unless your false
is not 0). The default +alloc
/+allocWithZone:
method will automatically zero out all ivars.
From https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW17
- It initializes all other instance variables to zero (or to the equivalent type for zero, such as
nil
,NULL
, and0.0
).
Also worth noting is that if you're doing Objective-C++, C++ objects that are ivars of Objective-C objects are not initalized : their constructors are not called by default.
精彩评论