处理自动合成属性时如何使用dealloc?
我对iOS开发相对比较陌生,请原谅,如果这是一个迟缓的问题。 我读过这个,但还是有点困惑。
我没有使用ARC。 (是的,我知道我应该,但我不在这一点)在我的课堂标题,我有这个
/*-----------------------------------------------------------------------+ | The name of the sender/receiver +-----------------------------------------------------------------------*/ @property (nonatomic, retain) NSString *name;
我不综合这个variables,但让编译器做这个工作。
以下哪项被认为是dealloc
方法的最佳实践
#1 Dealloc的iVar
-(void) dealloc { [_name release]; [super dealloc]; }
#2 Dealloc的财产
-(void) dealloc { [self.name release]; [super dealloc]; }
#3还有最后一个问题。 在dealloc
方法中习惯性地将property
设置nil
? 即
-(void) dealloc { [self.name release]; self.name = nil; [super dealloc]; }
如果有人能向我解释这一点真的很感激。
问候!
Jeff Lamarche写了一篇关于在dealloc中释放variables的好文章: http : //iphonedevelopment.blogspot.nl/2010/09/dealloc.html
他build议不要使用self.
语法,因为它可能会在multithreading环境中导致问题。
他的build议是使用iVar
并在生产版本中设置nil
:
-(void) dealloc { [_name release], _name = nil; [super dealloc]; }
方法1:
使用它是安全的。 在dealloc方法中释放iVar没有任何坏处。 但是,当您将值赋给名称时,必须通过属性或通过alloc方法(而不是工厂方法)。
方法2:
在发布的时候,不要使用属性。
方法3:
不要使用财产,但你肯定可以将零分配给伊娃。