对于不可变的 NSObject 后代,使用复制而不是保留好吗?

发布于 2024-12-19 11:45:00 字数 309 浏览 3 评论 0原文

我正在上一堂课,

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把昨日还给我 2024-12-26 11:45:00

事实上,许多人认为处理不可变对象的最正确方法是通过复制而不是保留。看看关于SO的讨论

关于内存管理的影响,就你而言不要忘记release那些复制的对象,你没问题。当然,如果您复制很多对象而不是保留它们,则您的内存消耗将在本地增加(因为在释放它们之前您将有副本)。

Indeed, many people think that the most correct way to handle immutable oblejcts is through copy and not retain. 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文