Should I initialize ALL primitive data types to 'safe' values in Obj-C?
-
22-09-2019 - |
Question
Is there any primitive data type that it's safe to not initialize?
How about structs like CGPoints or NSRects?
Solution
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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow