开发者

Should I initialize ALL primitive data types to 'safe' values in Obj-C?

开发者 https://www.devze.com 2022-12-08 15:37 出处:网络
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 a

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.

0

精彩评论

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