1

What code is correct and why ?

- (void)viewDidLoad 
{
    /*my code
     */
    [super viewDidLoad];
}

or

- (void)viewDidLoad 
{
    [super viewDidLoad];
    /*my code
     */
}
Voloda2
  • 12,359
  • 18
  • 80
  • 130

3 Answers3

1

It doesn't really matter that much. It's more about the way you'd like it. Would you want the super to respond first or the self? If it doesn't really matter that hard, do what you like.

v1Axvw
  • 3,054
  • 3
  • 26
  • 40
  • 1
    There is a situation where it definitely matters - dealloc. __Always put [super dealloc] at the end__. Otherwise you might crash. – deanWombourne Feb 06 '11 at 18:46
  • 1
    Sometimes it needs to be first, sometimes it needs to be last. Sometimes it doesn't matter. *Always* check the documentation as they documentation will note requirements, if any. In this specific case, it doesn't matter beyond that you do call it. – bbum Feb 06 '11 at 18:53
  • @deanWombourne: +1: Obvious, though very important – v1Axvw Feb 06 '11 at 19:11
0

It depends on whether you want your subclasses code to execute before or after the superclasses code for that method. I would say it's more common to do your own custom code after the call to super so that your subclasses code follows the superclasses code. Again, it depends on exactly what your trying to do.

Nitrex88
  • 2,158
  • 2
  • 21
  • 23
0

I'd say the latter. You want your superclass's code to run first before you run your own.

Or, if you're completely replacing the function, you'd just comment out the call to the superclass's implementation.

James Love
  • 1,025
  • 9
  • 16
  • In cases where it doesn't matter, "before self construction, after self destruction" is the write answer. Replacing an implementation entirely -- not calling super -- is only valid when documented as being valid (i.e. `drawRect:`) – bbum Feb 06 '11 at 18:55