开发者

Why is there a call to the superclass inside of many methods in Objective-C?

I wo开发者_如何学JAVAnder why in these examples there is always a [super someMethod] inside of the Method that has the exact same name:

- (void)viewDidLoad {
    [super viewDidLoad];
    // some code
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; 
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

I know that those messages are sent to the superclass, but what's the deal with this?


This is simply so that the superclass can do what it always does. It's a way for both your class and the superclass to receive the same message. In some situations, you are required to implement your own behaviour and invoke the behaviour of the superclass.

For example, the superclass will have its own memory cleanup routines which would otherwise be ignored if you did not invoke [super didReceiveMemoryWarning].


This is done to make sure that any superclass code (functionality and side effects) execute as well as what is in the derived class function. In effect, you are layering on top of base class functionality by doing this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜