对于不可变的 NSObject 后代,使用复制而不是保留好吗?
我正在上一堂课,
@interface SampleClas : NSObject
@property (nonatomic, retain) NSString *var1;
@property (nonatomic, retain) NSArray *var2;
@property (nonatomic, retain ) NSDictionary *var3;
@property (nonatomic, retain ) NSDate *var4;
@end;
我可以使用复制而不是保留吗?这不会以任何方式影响内存管理吗?
I am having a class as this
@interface SampleClas : NSObject
@property (nonatomic, retain) NSString *var1;
@property (nonatomic, retain) NSArray *var2;
@property (nonatomic, retain ) NSDictionary *var3;
@property (nonatomic, retain ) NSDate *var4;
@end;
Can I use copy instead of retain? Will this not affect memory management in any way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,许多人认为处理不可变对象的最正确方法是通过复制而不是保留。看看关于SO的讨论
关于内存管理的影响,就你而言不要忘记
release
那些复制的对象,你没问题。当然,如果您复制很多对象而不是保留它们,则您的内存消耗将在本地增加(因为在释放它们之前您将有副本)。Indeed, many people think that the most correct way to handle immutable oblejcts is through
copy
and notretain
. Have a look at this discussion on S.O.About, memory management implications, as far as you don't forget to
release
those copied objects, you are fine. Of course, if you copy very many objects instead of retaining them, your memory consumption will grow locally (because you will have copies around until you release them).