开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜