Obj-C: super class call

- (void)viewDidAppear:(BOOL)animated { <CODE BEFORE> [super viewDidAppear:animated]; <CODE AFTER> } 

What is right, to put all the code before or after the super call? It works in both directions, but I don’t know if it would be better to wait until the end of the call or send it at the beginning?

cheers endo

+4
source share
4 answers

In general, your code should go after calling super. The only obvious exception is dealloc, in which case you want to call [super dealloc] after you have cleaned up after yourself.

+3
source

The general rule is to first call it when setting up (for example, here) and call it last when things break.

+9
source

It depends on the specific case:

  • For initialization / cleanup, obviously, since the subclass depends on its state of the superclass, it must first initialize and clear.

  • In general, you may need to add behavior both before and after super-calls, or even completely omit the super-call (which in any case is to override the method).

In this particular case, see other answers; but since viewDidAppear: is a notification-like method, it really depends on whether your code needs a fully initialized object or is involved in the initialization itself, and therefore it should only make a super call as soon as it is done.

+1
source

It depends on what you do. Can you provide some context around the objects you use?

For example, in the context of destroying an object, you call super last.

 - (void)dealloc { [someObj release]; [super dealloc]; 

}

0
source

Source: https://habr.com/ru/post/1336351/


All Articles