objective-c - 自动化 dealloc/viewDidUnload Objective-C

标签 objective-c memory-management dealloc

这两个片段完成同样的事情吗?假设我的界面文件中有三个 IBOutlet UIButton,分别称为“buttonOne”、“buttonTwo”和“buttonThree”:

- (void)dealloc {
    for(UIButton* idx in self.view.subviews)
        [idx release], idx = nil;

    [super dealloc];
}

- (void)dealloc {
    [buttonOne release], buttonOne = nil;
    [buttonTwo release], buttonTwo = nil;
    [buttonThree release], buttonThree = nil;
    [super dealloc];
}

编辑:

由于 ARC 有时看起来像是 iOS 中内存管理的万能替代方案,我不想使用它,因为 a) 我觉得我在作弊 b) 如果我没记错的话,它只适用于 iOS 5设备。

最佳答案

假设您没有任何其他 subview ,这两段代码会执行相同的操作。但这不是你应该做的事情。

您应该声明您的导出weak(如果使用ARC)或分配(如果不使用ARC)。那么你就不必在dealloc中释放它们。 UIView 保留其 subview 并在释放它们时释放它们,因此您不需要保留或释放它们。您只需释放 self.view (或者,如果您是 UIViewController 的子类,则让 [super dealloc] 负责释放 self.view)。

关于objective-c - 自动化 dealloc/viewDidUnload Objective-C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8176601/

相关文章:

ios - 从 Swift 生成的 Objective-C header

c# - 以字节为单位的字典对象的大小

c++ - 在 C++ 中实现工厂方法的首选方法是什么?

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

ios - 带 block 参数的 NSInvocation

objective-c - 按下按钮时出现无法识别的选择器错误

objective-c - 如何将框架添加到xcode

ios - 快速上传视频并播放,无需缓冲来自 URL 的视频

iPhone内存管理

objective-c - iOS 中的 Dealloc 方法并将对象设置为 nil