为什么我需要在 iOs 中使用 [super %methodname%]? [英] Why I need to use [super %methodname%] in iOs?

查看:77
本文介绍了为什么我需要在 iOs 中使用 [super %methodname%]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Objective-C 的新手,不明白为什么我们需要使用 [super dealloc][super viewDidLoad][super viewWillAppear:animated].当我创建示例代码应用程序时,我看到如下内容:

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];
}

实际上 Xcode 4 总是在每个自动生成的末尾添加 super 方法方法.为什么?

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

或者当我使用 dealloc 方法时. 为什么我需要在最后添加 [super dealloc]?

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];
}

附言现在我学习开始 iPhone 4 开发".并且没有找到有关此方法的任何参考:(

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

推荐答案

重点是,您是通过继承它们来创建子类,在您的情况下,这似乎是一个自定义 ViewController,例如MyViewController 从 UIViewController 继承数据和方法.继承意味着,您的类将拥有父类拥有的所有方法,即使您没有指定它们.例如:

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

那么以下是有效的

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

两者的实现是

@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

如果没有 [super doSomething],超级重要的方法将永远不会被调用,因为您在自定义实现中覆盖了它.所以当你做一个

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];

Foo 的 doSomething 中包含的非常重要的代码不会被编译器看到,因为您自己在类中提供了该方法,因此只会调用 Bar 的版本.

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.

所以每当你重写一个方法时,你必须确保基类版本被调用,如果它必须被调用,这对于 dealloc 方法尤其重要,因为这将释放任何内存基类可能在初始化期间获得.

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.

这篇关于为什么我需要在 iOs 中使用 [super %methodname%]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆