我该什么时候打电话给超级 [英] When should I call super?
问题描述
[super ...任何方法名称]
的最佳使用方法是什么?最近我发现在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.
那么使用super方法的最佳方法是什么,例如最好用于 - (void)viewWillAppear:(BOOL)动画
。 [super viewWillAppear:(BOOL)动画]
在正文开头或最后的最佳位置在哪里?
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,然后做你的东西。当你覆盖某种拆解方法时,你会打电话给最后一个:
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屋!