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

yrvaken2

macrumors newbie
Original poster
Sep 15, 2008
12
0
If you create a UITableView Controller Subclass in Xcode, the viewDidLoad have the following code:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
}

what does this "super" mean, when do you use that?

Thanks Anders
 

russellelly

macrumors regular
Jun 23, 2006
139
41
Glasgow, UK
It's sending a message to do the 'viewDidLoad' method in the superclass (ie UIView) as well as what you've written in that class (since writing a viewDidLoad method will over-ride what's in UIView).
 

SqueegyX

macrumors regular
Mar 24, 2008
108
1
Lets say you have 2 class, a Parent and a Child. Child inherits from Parent. They have a method called greet which returns a string.

Here is what the parent method looks like:

Code:
-(NSString *)greet {
  return @"Hello";
}

We want the child to learn from his parents. So we use super to say greet how Mommy would greet, but with our own little additions too.

Code:
// Inherits from Parent
-(NSString *)greet {
  NSString *parentGreeting = [super greet];
  return [parentGreeting stringByAppendingString:@", Mommy"]
}

So now Parent greets "Hello", and the Child greets "Hello, Mommy". Later on, if we change the parent's greet to return just "Hi", then both classes will be affected and you will have "Hi" and "Hi, Mommy".

super is used to call a method as defined by a superclass. It is used to access methods that have been overriden by subclasses so that the class can wrap its own code around a method that it's parent class implements. It's very handy if you are doing any sort of inheritance at all.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.