我对 iOS 开发还比较陌生,所以如果这是一个弱智问题,请原谅。我读过this但我还是有点困惑。
我没有使用 ARC。 (是的,我知道我应该,但我现在不这样做)在我的类标题中,我有这个
/*-----------------------------------------------------------------------+
| The name of the sender/receiver
+-----------------------------------------------------------------------*/
@property (nonatomic, retain) NSString *name;
我不合成这个变量,而是让编译器完成这项工作。
以下哪项被认为是 dealloc
方法的最佳实践
#1 释放 iVar
-(void) dealloc {
[_name release];
[super dealloc];
}
#2 解除分配该属性
-(void) dealloc {
[self.name release];
[super dealloc];
}
#3 最后一个问题。是否习惯在 dealloc
方法中将 property
设置为 nil
?即
-(void) dealloc {
[self.name release];
self.name = nil;
[super dealloc];
}
如果有人能向我解释这一点,我将非常感激。
问候!
最佳答案
Jeff Lamarche 写了一篇关于在 dealloc 中释放变量的好文章: http://iphonedevelopment.blogspot.nl/2010/09/dealloc.html
他建议永远不要使用 self.
语法,因为它可能会在多线程环境中导致问题。
他的建议是使用 iVar
并在生产版本中设置为 nil
:
-(void) dealloc {
[_name release], _name = nil;
[super dealloc];
}
关于ios - 处理自动合成的属性时如何使用dealloc?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15107440/