Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
hi.
this code is from beginning iphone development
- (void)viewDidLoad {
......
UIApplication *app=[UIApplication sharedApplication];
......

[super viewDidLoad];
}

my question is that why did not the author make [super viewDidLoad] the first statement. according to some rules, we should set up the things from its superclass and then itself. the author did so in both versions of his books. is there a special purpose?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
Unfortunately there's not a good answer to this question. In the apple docs in some cases it's made clear that the base class implementation of a method does something important and in other cases it's made clear that the base class method does nothing important. In most cases it isn't mentioned. However, in the apple iPhone forums some of the apple engineers sometimes emphasize that user code should call the base class implementation of this method and others like it but no clear reason is offered.

FWIW, I've never called super viewDidLoad in my code and I'm, not aware of any problems because of this. My assumption is that UIViewController viewDidLoad doesn't do anything. I might be wrong about that and they might change things in the future. Such is life on this platform.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
is there a special purpose?
Probably not. I would bet the supers aren't doing anything special (if at all) in their viewDidLoads and so it doesn't make any difference. But, as you mention, it's normally good practice to let the super handle its duties first before you add yours.

You might try asking the authors of the book, at their website, if this was just by accident or there was a reason.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Also, just wanted to add a comment about a similar (and new) method: viewDidUnload. Inside that you probably want to be putting your code before the [super viewDidUnload];
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
viewDidUnload is exactly the kind of method that I was talking about. The docs mention nothing about needing to call super from that method and its purpose makes me think that there's no need to call super. FWIW, I don't call super from my code and I don't see any problems.

If you had a hierarchy of view controllers, that is you have a view controller that inherits from another one of your own view controllers, then it would make sense for the derived class to call super. Otherwise I don't see any reason to do that.

Just to add, if I look through Apple's sample code it's a mixed bag. Some of it calls super from viewDidLoad/viewDidUnload and some of it doesn't.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
If you had a hierarchy of view controllers, that is you have a view controller that inherits from another one of your own view controllers, then it would make sense for the derived class to call super. Otherwise I don't see any reason to do that.
Actually, Chapter 9: Navigation Controllers and Table Views from the "Beginning iPhone Development" book does just that. You create a SecondLevelViewController class that's like an abstract class and then subclass that class for each of the six second level views.

And if calling the super doesn't hurt most of the time, I don't see any reason not to just include it, just in case. Unless you have specific reason that calling it is causing an issue.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
And if calling the super doesn't hurt most of the time, I don't see any reason not to just include it, just in case.

My logic is the opposite. Why add it if it adds no known benefit and the docs don't require it? It's just unneeded overhead.

IMO, this is some kind of laziness on Apple's part, or maybe just an internal disagreement. Adding code because of 'just in case' doesn't make any sense to me.
 

firewood

macrumors G3
Jul 29, 2003
8,108
1,345
Silicon Valley
If the super is initializing something you need to use or are modifying (alloc-ing stuff you are about to write to), put it first, or you will trash memory or crash.

If the super is cleaning up (releasing stuff are using), put it at the end, or you will trash memory, over-release or leak stuff.

If none of the above, why bother?

If you don't know, you could have a major problem.
 

Howiieque

macrumors regular
Original poster
Feb 1, 2009
120
0
thank you firewood, your lines really makes sense.
i don't know what his super class will do. its super class is UIViewController.
but i think it might fall into the first case, and [super viewDidLoad is going to the first statement.
thanks all of you.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.