3

I'm newbie in Objective-C, and don't understand why we need to use [super dealloc], [super viewDidLoad] or [super viewWillAppear:animated]. When I create sample code application I see something like this:

- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}

 - (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

Actually Xcode 4 always add super method at the end for every automatic generated method. Why?

Or when I use dealloc method. Why I need to add [super dealloc] at the end?

- (void)dealloc
{
 [nameField release];
 [numberField release];
 [sliderLabel release];
 [super dealloc];
}

P.S. Now I study "Beginning iPhone 4 Development". And don't find any reference about this method :(

Alex Lapchenko
  • 283
  • 1
  • 5
  • 14
  • see [this question](http://stackoverflow.com/questions/3095360/what-exactly-is-super-in-objective-c) – Nerdtron Feb 10 '12 at 20:00

3 Answers3

10

The point is, that you are creating subclasses by inheriting them, in your case this seems to be a custom ViewController, e.g. MyViewController which inherits data and methods from UIViewController. Inheritance means, your class will have all the methods the parent class have, even if you don't specify them. For example:

@interface Foo : NSObject
- (void) doSomething;
@end

@interface Bar : Foo
@end

Then the following is valid

Bar *myBar = [[Bar alloc] init];
[myBar doSomething];

Even if you havent declared the method, it is found in the superclass and thus the superclasses method is called.

Now suppose you have the following:

@interface Bar : Foo
-(void) doSomething;
@end

And the implementation for both is

@implementation Foo
- (void) doSomething
{
  NSLog(@"This is a super important method that HAS TO BE CALLED IN ANY CASE");
}
@end

@implementation Bar
-(void) doSomething
{
 NSLog(@"Well this is important, but not as important as Foos implementation");
}
@end

Without a [super doSomething], the super important method will never be called since you have overridden it in your custom implementation. So when you do a

Bar *myBar = [[Bar alloc] init];
[myBar doSomething];

the totally important code contained in Foo's doSomething won't be seen by the compiler as you provide the method in your class yourself, so only Bar's version will be called.

So whenever you override a method, you have to make sure that the base class version is called if it must be called, which is especially important for the dealloc method as this will release any memory the base class might have acquired during initialization.

cli_hlt
  • 7,072
  • 2
  • 26
  • 22
4

In most cases, when you override a method in the parent class, for example UIViewController, you still want the default behavior to happen as well as the custom behavior you specify. [super method-name] is how you tell the app you want the parent's implementation to be included.

If the only line in the method is a call to super, you can just omit the entire method from your class since all you want is the default behavior defined by the parent.

For example, including this serves no purpose and can be removed from your code:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}
picciano
  • 22,341
  • 9
  • 69
  • 82
3

The call to [super dealloc], for example, tells the super-class to deallocate any instance variables that it allocated. In your subclass you dealloc the instance methods the subclass allocated, but then the super class needs to do the same, so you have to explicitly call it. Similarly, with the other methods, you may need the underlying functionality they provide in addition to the functionality you are providing. (Though in some cases, you may not.) It's just a way to tell the compiler that you want the super class to do what it usually does in addition to what your class is doing.

user1118321
  • 25,567
  • 4
  • 55
  • 86