将呼叫设置为超级? [英] Placing calls to super?

查看:114
本文介绍了将呼叫设置为超级?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前问过这个问题,但是我看的其他代码示例越多,我看到布局的差异。通常我尝试在开始(如果它的一个创建类型方法)或结束(如果它的一个销毁类型方法)(见下面...)调用超级调用

   - (void)viewDidLoad {
[super viewDidLoad];
...
...
}

/ p>

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

我看到的另一种方法似乎总是调用super的方法。 (见下面...)我的问题是它是很重要吗?

   - (void)viewDidLoad {
.. 。
...
[super viewDidLoad];
}

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

cheers gary。




  • 该方法是初始化的实例变量( viewDidLoad 通常不会这样做,因为 init method的工作),在这种情况下 super 调用应该在开始。

  • 实例变量的释放,在这种情况下 super 调用应该在结束。

  • 某种事件,例如通过响应者链,在这种情况下,你实际上不会使用超级调用(但模式是类似的),你会把调用在



我可能错过了一些重要的情况。


I have asked this before, but the more I look at other code examples the more I see differences in layout. Usually I try and put the call to super at the start (if its a creation type method) or at the end (if its a destroy type method) (see below...)

-(void)viewDidLoad {
   [super viewDidLoad];
   ...
   ...
}

.

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

The other method I see always seems to place the call to super at the end of the method regardless. (see below...) my question is does it matter?

-(void)viewDidLoad {
   ...
   ...
   [super viewDidLoad];
}

.

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

cheers gary.

解决方案

I'd say that it doesn't usually matter, unless:

  • The method is doing initialisation of instance variables (which viewDidLoad usually wouldn't be doing, as that's the init method's job), in which case the super call should be at the start.
  • The method is doing deallocation of instance variables, in which case the super call should be at the end.
  • The method is passing on some sort of event, e.g. through a responder chain, in which case you wouldn't actually be using a super call at all (but the pattern is similar), and you'd put the call at the end.

I may have missed some important cases, though.

这篇关于将呼叫设置为超级?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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