如果手动实现setter,是否需要(retain)?
我想知道下面的属性声明中的retain有什么作用(如果有的话)。手动实现的设置器保留变量。
接口:
@property (nonatomic, retain, setter=setIncident:) Incident * incident;
实现:
- (void)setIncident:(CSIncident *)newIncident
{
if (incident != newIncident)
{
[incident release];
incident = [newIncident retain];
}
}
I would like to know what effect (if any) does the retain in the following property declaration have. The manually implemented setter is retaining the variable.
Interface:
@property (nonatomic, retain, setter=setIncident:) Incident * incident;
Implementation:
- (void)setIncident:(CSIncident *)newIncident
{
if (incident != newIncident)
{
[incident release];
incident = [newIncident retain];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它没有太大价值,因为您手动控制设置的行为并手动保留它。
唯一的(非常小的)值是当您只查看标题时理解行为的可读性,并且如果删除手动实现(但您已经必须删除 setter=),则会减少流失。
在您的问题中,setDog 和 setIncident 之间也不匹配
It doesn't have much value since you're manually controlling the set behavior and retaining it manually.
The only (very minimal) value would be readability to understand the behavior when you're just looking at the header and less churn if you remove the manual implementation (but you already have to remove setter=).
In your question you also have a mismatch between setDog and setIncident