ios - 处理自动合成的属性时如何使用dealloc?

标签 ios objective-c dealloc

我对 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/

相关文章:

ios - 我的 TableViewController 中的filteredUsers 的数据源应该是什么?

ios - UIScrollView 作为另一个 UIScrollView 的 subview

ios - UIPasteboard 未清理

ios - 在 UITableViewCell 中进入编辑模式时隐藏 UITableViewCells(类似于联系人应用程序)

iphone - 在导航 Controller 中按下后退按钮时是否可以不释放 View Controller ?

ios - UITextView 类中未调用 UIAlertView 委托(delegate)方法

iphone - 显示问题的MKA注释

ios - 在 iPhone 上播放直播视频

iphone - 我什么时候释放一个对象?

ios - didReceiveMemoryWarning 和 dealloc 之间有什么关系?