Should I initialize ALL primitive data types to 'safe' values in Obj-C?
Is there any primitive data type that it's safe to not initialize?
How about structs like CGPoints or NSRects开发者_JAVA百科?
It depends on where the variable is stored. The language specifies that all objects are zero'ed on alloc, which means all the ivars will be 0 filled. For any primitive type where the backing store being 0 makes sense then it is perfectly safe. For instance:
@interface LGDemo : NSObject {
CGPoint point;
NSRect rect;
}
@end
It is perfectly safe not to explicitly init point or rect, after the object is alloc'ed they will be {0.0, 0.0} and {0.0, 0.0, 0.0, 0.0} respectively.
精彩评论