自动化dealloc/viewDidUnload Objective-C

eri*_*ell 2 memory-management objective-c dealloc

这两个片段是否完成了同样的事情?假设我的界面文件中有三个名为buttonOne,buttonTwo和buttonThree的IBOutlet UIButton:

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

    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

- (void)dealloc {
    [buttonOne release], buttonOne = nil;
    [buttonTwo release], buttonTwo = nil;
    [buttonThree release], buttonThree = nil;
    [super dealloc];
}
Run Code Online (Sandbox Code Playgroud)

编辑:

由于ARC有时似乎是iOS中内存管理的替代方案,我更喜欢不使用它,因为a)我觉得我在欺骗b)如果我没有弄错,它只适用于iOS 5设备.

rob*_*off 5

假设您没有任何其他子视图,那么这两段代码就会做同样的事情.但这不是你应该做的事情.

您应该声明您的销售点weak(如果使用ARC)或assign(如果不使用ARC).然后你不必释放它们dealloc.A UIView保留其子视图并在取消分配时释放它们,因此您无需保留或释放它们.你刚刚发布self.view(或者,如果你是一个子类UIViewController,你[super dealloc]可以放心发布self.view).