可能重复:
Why shouldn't I use Objective C 2.0 accessors in init/dealloc?
dealloc, use release or set to nil for properties?
您好,
Apple通常在dealloc中发布ivars但是在dealloc中使用nilling属性是否有任何问题?
我的意思是代替:
- (void) dealoc(){
[myRetainedProperty release];
[super dealloc];
}
写这样的代码:
- (void) dealoc(){
self.myRetainedProperty = nil;
[super dealloc];
}
我知道这是一个额外的方法调用,但另一方面它更安全,因为当您将属性表单retain
更改为assign
并且忘记修改dealloc时它不会崩溃。< / p>
你怎么看?除了表现之外,您能否考虑使用发布而不是设置为nil的任何其他原因?
更新 这个问题似乎与"Why shouldn't I use Objective C 2.0 accessors in init/dealloc?"重复。
答案 0 :(得分:2)
答案 1 :(得分:1)
我使用“发布”风格已经有一段时间了,之后我转而将属性设置为零。 我想如果你刚刚发布了ivar那么你有机会在dealloc中重复使用它,这可能不会发生但仍然...... 另外我认为将属性设置为nil更符合“用属性封装你的ivars”理论。
所以我认为更好地使用“设置为零”样式,但是我不知道仅使用发布的任何真正原因......
Moszi
答案 2 :(得分:0)
是的,分配nil看起来更好,直到你忘记写自己。在属性名称之前:)