2

Possible Duplicate:
When should I call super?

Is there is difference/preferred way when placing calls to the super class methods like [super viewDidLoad] or [super init] or [super viewDidUnload]? I have seen tutorials putting them either in the first line or as the last line in a child class method. I am just wondering if there would be any dependency like if the super class method would do some setup the child could use when called upfront or if the super could do some optimizations to something the child has done when called at the end.

Community
  • 1
  • 1
  • 1
    possible duplicate of [When should I call super?](http://stackoverflow.com/questions/3906704/), [Should I call the \[super superMethod\] after my own code or before it?](http://stackoverflow.com/questions/7441519/), [Top or bottom super call in overridden method](http://stackoverflow.com/questions/12019031/), [Placing calls to super](http://stackoverflow.com/questions/3466889/), [Does the timing of calling the method of the super class matter in ObjectiveC?](http://stackoverflow.com/questions/1005196/) – jscs Sep 10 '12 at 05:19

2 Answers2

4

My general rule of thumb is that acts of construction/creation call through super first, while acts of destruction or tearing down state happen in reverse order -- super is called last. There's rarely a need to deviate from that.

The reason is not typically for optimization (as asked in your post), but for predictable semantics.

Example A:

construction:

- (id)init
{
  self = [super init]; // << set up super
  if (0 != self) {
    // set up self
  ...

destruction:

- (void)dealloc
{
  [ivar release], ivar = 0; // << tear down self
  [super dealloc]; // << tear down super

Example B:

construction:

- (void)viewWillAppear:(BOOL)pAnimated
{
  [super viewWillAppear:pAnimated]; // << call through super first
  // now set up self
  ...

destruction:

- (void)viewDidDisappear:(BOOL)pAnimated
{
  // tear down self
  [super viewDidDisappear:pAnimated]; // << now tear down super
justin
  • 104,054
  • 14
  • 179
  • 226
0

You must initialise the super first, because the super constructor has the option to return a value other than the original value of self.

self = [super init];

If you initialise other properties of self before calling [super init], these changes may be erased.

What on earth is that for?

@Justin notes that Class Clusters (used when returning instances of NSDictionary or NSString) uses this mechanism to provide a slightly different subclass depending upon the task. Singletons can also use this, and the (now deprecated) Pose function of Objective-C could as well.

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
  • I don't know whether this ever actually happens in practice, but you have to abide by the rules. – Alex Brown Sep 10 '12 at 05:18
  • it does happen. class clusters and singletons are the notable examples. – justin Sep 10 '12 at 05:20
  • I thought about singletons, and then a cold chill ran through me. It doesn't seem like a good idea to initialise subclasses after receiving a singleton - the original might have been of a different subclass, or have running data. – Alex Brown Sep 10 '12 at 05:26
  • Class clusters - https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/ClassCluster.htmlOh, that seems reasonable. – Alex Brown Sep 10 '12 at 05:28
  • i don't like singletons, but that doesn't stop people from writing them :) one initialization pattern of an objc singleton implementation will just stash the first instance created -- if a new instance is requested, the first instance is returned. however, that design is practically sealed to subclassing. – justin Sep 10 '12 at 05:33
  • yes, class clusters are reasonable. however, it's not normal for many people to create and derive from leaves of class clusters -- so it's unusual to actually be returned another instance from the superclass' initializer at the abstraction level most of us work at (unless you happen to be developing class clusters). – justin Sep 10 '12 at 05:37
  • 1
    Some of this flew over my head, but thank you @justin and Alex-Brown for helping me out. I have a better understanding on how this should be implemented now, as justin mentioned in the first answer. – Deepu Mukundan Sep 10 '12 at 17:20