should I call the [super superMethod] after my own

2019-06-20 16:42发布

[sorry for my weak english]

The question is simple (but I have troubles in expressing it and finding it in google)...

Should I (in all similar cases, when I override the super method, not only this one) use:

- (void)viewDidLoad
{
    /*
       my code
     */

    [super viewDidLoad];
}

or

- (void)viewDidLoad
{
    [super viewDidLoad];

    /*
       my code
     */
}

or does it depend?

2条回答
可以哭但决不认输i
2楼-- · 2019-06-20 17:34

in some cases the order will not matter. in others, order is critical.

some generalizations which will help:

  • when you are constructing a portion of the object's state (viewDidLoad and init...), call through super first.
  • when you are destructing a portion of the object's state (viewDidUnload or dealloc), call through super last.
  • if i am certain that the order does not matter, then i just call through super first for easier organization.
查看更多
beautiful°
3楼-- · 2019-06-20 17:44

It depends on the method; e.g. -[super init] (before) vs -[super dealloc] (after).

查看更多
登录 后发表回答