26

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?

Mxyk
  • 10,678
  • 16
  • 57
  • 76

3 Answers3

59

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.

zoul
  • 102,279
  • 44
  • 260
  • 354
18

Just check the corresponding documentations. For instance, when to call super in overridden methods of UIViewController:

didReceiveMemoryWarning : You can override this method (as needed) to release any additional memory used by your view controller. If you do, be sure to call the super implementation at some point to allow the view controller to release its view. [Means the order is of no importance.]

loadView : Your custom implementation of this method should not call super.

setEditing:animated : This method should invoke super’s implementation before updating its view. [Means the order is of importance.]

viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear: If you override this method, you must call super at some point in your implementation. [Means the order is of no importance.]

dealloc: If you implement this method but are building your application for iOS 2.x, your dealloc method should release each object but should also set the reference to that object to nil before calling super. [Means the order is of importance.]

Did you realize similar rules for super in viewDidLoad and viewDidUnload methods aren't mentioned? Because you don't need to call super in these.

Özgür
  • 8,077
  • 2
  • 68
  • 66
  • 2
    A word of caution: One must be careful when dealing with deeper hierarchies. If A inherits from UIVIewController and overrides viewDidLoad, and B inherits from A and also overrides viewDidLoad, it could be the case that sending [super viewDidLoad] in B’s implementation is mandatory. As Comptrol said, the documentation of A should make that explicit and the developer implementing B should read A’s documentation. –  Jan 03 '11 at 17:02
3

It mostly depends on whether your subclass needs things to happen before the superclass method, or after. Or both, as the case may be.

That's why init methods call the super method first thing, and dealloc methods last thing. In your example, when you are being notified that a view will appear, you might want to let the super do its thing, and then, after that, take care of whatever your subclass view needs to do. But it could conceivably be other way around, or you could call the super method in between your code. Different rules will apply according to your application's needs.

Victor Jalencas
  • 1,216
  • 11
  • 23