为什么我必须最后调用 super -dealloc,而不是先调用? [英] Why do I have to call super -dealloc last, and not first?

查看:20
本文介绍了为什么我必须最后调用 super -dealloc,而不是先调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正确的例子:

- (void)dealloc {
    [viewController release];
    [window release];
    [super dealloc];
}

错误的例子:

- (void)dealloc {
    [super dealloc];
    [viewController release];
    [window release];
}

Although 在几乎所有其他情况下,当覆盖一个方法时,我会首先调用 super 的方法实现,在这种情况下,Apple 总是在最后调用 [super dealloc].为什么?

Althoug in alsmost all other cases when overriding a method I would first call the super's method implementation, in this case apple always calls [super dealloc] in the end. Why?

推荐答案

这只是一个指导方针.你可以在[super dealloc]之后调用其他指令.但是您不能再访问超类的变量,因为它们在您调用 [super dealloc] 时被释放.在最后一行调用超类总是安全的.

Its just a guideline. You can call other instructions after [super dealloc]. however you can not access variables of the superclass anymore because they are released when you call [super dealloc]. It is always safe to call the superclass in the last line.

如果 KVO 和依赖(触发)键依赖于已发布的成员变量,它们也会产生副作用.

Also KVO and depended (triggered) keys can produce side effects if they are depended of already released member variables.

这篇关于为什么我必须最后调用 super -dealloc,而不是先调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆