Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I call super?

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.

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?

like image 615
Alexander Slabinsky Avatar asked Sep 10 '25 16:09

Alexander Slabinsky


1 Answers

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];
}

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.

like image 165
zoul Avatar answered Sep 15 '25 12:09

zoul