我什么时候应该打电话给超级? [英] When should I call super?
问题描述
[super ... any method name]
的最佳用法是什么.最近我发现在 dealloc 中 [super dealloc]
必须站在同一端.因为如果我们在 [super dealloc]
之后设置它,任何之前没有使用过的变量都会被垃圾填充.这是一种罕见的事情,但它是可能的.在此之后,我们的应用程序将崩溃.
What is the best using of [super ... any method name]
. Recently I have found out that In dealloc the [super dealloc]
must stand in the same end. Because any variable what didn't use before can be filled by garbage if we set it after [super dealloc]
It's a rare thing, but it's possible. After this we will have crash in our app.
那么什么是超级方法的最佳用途,例如什么是最好的用于-(void)viewWillAppear:(BOOL)animated
.[super viewWillAppear:(BOOL)animated]
正文开头或结尾的最佳位置在哪里?
So what is the best using of super method, for example what is best using for -(void)viewWillAppear:(BOOL)animated
. Where is the best place for [super viewWillAppear:(BOOL)animated]
in the begin of body or in the end?
推荐答案
通常的经验法则是,当你覆盖一个进行某种初始化的方法时,你首先调用 super 然后做你的事情.当你重写某种拆卸方法时,你调用 super last :
The usual rule of thumb is that when you are overriding a method that does some kind of initialization, you call super first and then do your stuff. And when you override some kind of teardown method, you call super last:
- (void) setupSomething {
[super setupSomething];
…
}
- (void) tearDownSomething {
…
[super tearDownSomething];
}
第一类是像 init...
、viewWillAppear
、viewDidLoad
或 setUp
这样的方法.第二个是诸如 dealloc
、viewDidUnload
、viewWillDisappear
或 tearDown
之类的东西.这不是硬性规定,它只是遵循方法所做的事情.
The first kind are methods like init…
, viewWillAppear
, viewDidLoad
or setUp
. The second are things like dealloc
, viewDidUnload
, viewWillDisappear
or tearDown
. This is no hard rule, it just follows from the things the methods do.
这篇关于我什么时候应该打电话给超级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!