60

I see some example code with [super viewDidLoad] called before your implementation and after your implementation.

I know you don't always have to call super (as seen in many other discussions). When you do call it, is it expected before or after you code?

This could have consequences depending on what super's implementation does. Though you shouldn't have to know super's implementation to write yours.

Of course this goes for all of UIViewControllers delegate methods (willAppear, didAppear, etc...)

Any thoughts?

Dan
  • 29,100
  • 43
  • 148
  • 207
Corey Floyd
  • 25,929
  • 31
  • 126
  • 154
  • related: http://stackoverflow.com/questions/824695/do-i-always-have-to-call-super-viewdidload-in-the-viewdidload-method – cregox Jan 12 '11 at 12:39

2 Answers2

98

My rule of thumb is: if you're doing something related to initialization, always call the super class's method first (if you are going to call it at all). This gives the super class a chance to do any set-up that you may be relying on later in your method. If you're doing something related to destruction, call the super class's method last. This makes sure that you can rely on the state of the object throughout the execution of your method. Finally, take any other case on a case-by-case basis. For instance, if you're handling an event, you probably want to deal with the event first, and only invoke the super class's method if you chose not to handle the event or if you somehow altered it and want to pass it along the event chain.

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
  • 7
    Good advice from Jason, and often Apple documents subclasser responsibility when it's important. E.g. when some crucial behavior depends on a super call, it's often highlighted in the documentation. In general, if it's not going to hurt you, just call through to super to be safe. – danielpunkass May 10 '09 at 04:55
  • Thanks, that is pretty logical and kind of the approach I was taking. I was subclassing my own classes and I had a choice of what to do. I didn't want to violate convention by requiring setup before/after viewDidLoad. – Corey Floyd May 11 '09 at 16:43
  • 3
    Of course its the opposite in -dealloc, since you might need to use something belonging to the superclass before it gets deallocated. – mk12 Sep 29 '09 at 00:05
0

Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.

Here is what the parent method looks like:

Code:

-(NSString *)greet {
 return @"Hello";
}

We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.

Code: // Inherits from Parent

-(NSString *)greet {
NSString *parentGreeting = [super greet];
return [parentGreeting stringByAppendingString:@", Mommy"]
}

So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".

super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.

Kishore Suthar
  • 2,943
  • 4
  • 26
  • 44