Why do I have to call super -dealloc last, and not first?(为什么我必须最后调用 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,而不是先调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我必须最后调用 super -dealloc,而不是先调用?
基础教程推荐
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
