endo.anaconda
endo.anaconda

Reputation: 2468

Obj-C: call super class

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

What is correct, to put all code before or after the super call ? Its working both ways, but I don't know if its better to wait for the call until the end or submit it at the beginning ?

cheers endo

Upvotes: 4

Views: 289

Answers (4)

Damien Pollet
Damien Pollet

Reputation: 6598

It depends on the specific case, really:

  • For initialization / cleanup, obviously, since the subclass depends on its superclass state, it should initialize after and cleanup before.

  • In general, you might very well need to add behavior both before and after the super-call, or even omit the super-call entirely (that's what method override is for, after all).

In this precise case, see the other answers; but since viewDidAppear: is a notification-like method, it really depends whether your code needs a fully initialized object, or takes part in the initialization itself and so must only proceed with the super-call once it's done.

Upvotes: 1

RyBolt
RyBolt

Reputation: 1794

It depends what you're doing. Can you provide some context around the objects you're using?

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

- (void)dealloc {
[someObj release];

[super dealloc];

}

Upvotes: 0

Eiko
Eiko

Reputation: 25632

The general rule of thumb is to call it first when setting things up (like here) and call it last when tearing things down.

Upvotes: 8

Reed Olsen
Reed Olsen

Reputation: 9169

In general, your code should go after the call to super. The one obvious exception is dealloc, in which case you want to call [super dealloc] after you've cleaned up after yourself.

Upvotes: 3

Related Questions