这行代码是否正确重新内存管理(重新 NSDate 复制)?
这行代码是否正确重新内存管理(重新 NSDate 复制)?
我有一个带有属性的类:
@property (nonatomic, retain) NSDate* start;
@property (nonatomic, retain) NSDate* coreWeStart;
现在在 init 方法中,假设已经设置了 self.start,将 coreWeStart 重新设置为同一日期是否正确:
self.coreWeStart= [[self.start copy] autorelease];
只需仔细检查我的理解:
- 需要一个“副本”,否则它会引用到同一个对象并且
- 需要自动释放,就像我做的复制一样,
谢谢
is this line of code correct re memory management (re NSDate copy)?
I have a class with properties:
@property (nonatomic, retain) NSDate* start;
@property (nonatomic, retain) NSDate* coreWeStart;
Now in the init method, assuming self.start is already set, is this correct re setting the coreWeStart to the same date:
self.coreWeStart= [[self.start copy] autorelease];
Just double checking my understanding that:
- needs a 'copy' as otherwise it would refer to the same object and
- needs an autorelease as I did do a copy
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会说有点,但仍然可以做得更好。具体来说,您可以执行以下操作:
...然后:
...用更少的代码获得相同的结果。还要确保在 dealloc 中执行
self.coreWeStart = nil
(以及self.start = nil
)。I would say kind of, but it could still be done better. Specifically, you could do:
...and then:
...to get the same thing with less code. Also be sure to do
self.coreWeStart = nil
in dealloc (andself.start = nil
too).是的。你明白了。
因此,您的对象具有保留计数为 1 的 coreWeStart 属性,它是 start 属性的副本。
Yep. You got it.
So your object has the coreWeStart property with a retain count of one, which is a copy of the start property.