3

I have a small question on what super means in xcode. For instance I have the following code. Would it work if i said [super dealloc] first in the dealloc method? Or should super always come last? What exactly is super - I know its the super class but is it the parent class or?

This is the .h file for this class

@interface TwitterTableViewController : PullRefreshTableViewController<TweetDelegate>

This is some code from the .m class for the above interface

- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
    // Custom initialization
}
return self;
}

- (void)dealloc
{

[self.twitterManager release];
[tweets release];
[lastRefreshedLabel release];
[super dealloc];

}

I have checked all over and dont have a satisfactory knowledge yet. If anyone can explain this in laymans terms would be best. thanks

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
CodeGeek123
  • 4,341
  • 8
  • 50
  • 79
  • If you are familiar with other programming languages think of it as **parent** – Bot Feb 28 '12 at 15:41
  • 3
    This isn't a small question. It's a big question. I would recommend making sure you are familiar with Objective-C before doing application development. – Mark Leonard Feb 28 '12 at 15:44
  • possible duplicate of [What exactly is super in Objective-C?](http://stackoverflow.com/questions/3095360/what-exactly-is-super-in-objective-c) | [When should I call super?](http://stackoverflow.com/questions/3906704/when-should-i-call-super) | [Is super local variable?](http://stackoverflow.com/questions/2962735/is-super-local-variable) | [Is it okay to write code after \[super dealloc\]?](http://stackoverflow.com/questions/4594305/is-it-ok-to-write-code-after-super-dealloc) – jscs Feb 28 '12 at 18:02

6 Answers6

5

super calls the implementation of the super-class. In your case this would be PullRefreshTableViewController.

Regarding if you should call super before/after your own code, it really depends on what you want to achieve by overriding the method.

In case of dealloc have a look at this question, as Nick Bull mentioned.

Community
  • 1
  • 1
  • 6
    Definitely _not_ for dealloc, though! It should always be last there, or you will call [super dealloc] all the way back to NSObject before you have finished releasing the rest of your ivars. – jrturton Feb 28 '12 at 15:40
  • OK, try it and see what happens! – jrturton Feb 28 '12 at 15:46
  • @ErikAigner see this answer http://stackoverflow.com/questions/909856/why-do-i-have-to-call-super-dealloc-last-and-not-first – Nick Bull Feb 28 '12 at 15:46
3

The super class can be regarded as a "parent class", yes, if you mean. [super dealloc] calls the implementation of the dealloc method of the super/parent class of the current object (or class, if you call it from a class method).

But, there's no such rule that "super must always come last". The reason why dealloc must always come last is that it destroys the object, so if you accessed your object after it returned, that would crash.

2

In addition to the existing answers here, the info in this answer may help you in deciding when to call "super".

The methods that are called when the child object/view/viewcontroller is created/initialized, the first thing you do is call the super (i.e., if you want to call it).

When the child object/view/viewcontroller is destroyed/removed, you call the super at the end of the method.

eg:

// methods called when the you are loading/showing with the view controller
    -(void)viewDidLoad {
        [super viewDidLoad];
        //call super and then all your code goes here
    }

    -(void)viewWillAppear:(BOOL)animated {
        [super viewWillAppear:animated];
        //call super and then all your code goes here
    }

// methods called when the you are done with the view controller
    - (void)viewWillDisappear:(BOOL)animated{
        //all your code goes here and then call super
        [super viewWillDisappear:animated];
    }
    - (void)viewDidUnload {
        //all your code goes here and then call super
        [super viewDidUnload];
    }

Similarly, for -dealloc, the super is called at the end, for -init super is called at the beginning.

Hope this demystifies Super.

Nitin Alabur
  • 5,812
  • 1
  • 34
  • 52
1

super stands for the super object / aka the father of the class you are impementing which extends

in your case is a PullRefreshTableViewController

this kind of behavior is the base of all the object oriented languages, like obj-c.

in the case you are analyzing the super class has a method called

 -(UITableviewStyle *) initWithStyle:style
holographix
  • 2,497
  • 2
  • 32
  • 46
  • So in the above case can i say - (void)dealloc { [super dealloc]; [self.twitterManager release]; [tweets release]; [lastRefreshedLabel release]; } – CodeGeek123 Feb 28 '12 at 15:40
  • 3
    @CodeGeek123 No, you can't do that. `[super dealloc]` has to be called at the end of the dealloc. See @jrturton comment in Erik's answer – Nick Bull Feb 28 '12 at 15:44
  • 4
    Precisely NOT! Don't call [super dealloc] until the _end_ of your own implementation of dealloc! See H2C03's answer. – jrturton Feb 28 '12 at 15:44
  • 1
    whoops yes, sorry didn't read all through :p [super dealloc] is always the last thing you want to call in the deallc method – holographix Feb 28 '12 at 15:46
1

super specifies that the messages should be sent to the parent class (superclass) of the current class. Since you're inheriting from PullRefreshTableViewController, that's where they'll get sent.

Ry-
  • 218,210
  • 55
  • 464
  • 476
1

super is the parent class, here PullRefreshTableViewController. Calling [super dealloc], which you should always do if you need to implement your own dealloc method, allows your parent class (and its parent class, and so on) to clean up after themselves. It's usually a good idea to perform your own releases, and only then let the parent class do the same, because you can never be sure what the parent class will sweep away from under your feet.

deleterOfWorlds
  • 552
  • 5
  • 9