Obj-C:调用超类
- (void)viewDidAppear:(BOOL)animated {
<CODE BEFORE>
[super viewDidAppear:animated];
<CODE AFTER>
}
将所有代码放在 super 调用之前或之后正确的做法是什么? 它是双向的,但我不知道是等待调用直到结束还是在开始时提交更好?
干杯远藤
- (void)viewDidAppear:(BOOL)animated {
<CODE BEFORE>
[super viewDidAppear:animated];
<CODE AFTER>
}
What is correct, to put all code before or after the super call ?
Its working both ways, but I don't know if its better to wait for the call until the end or submit it at the beginning ?
cheers endo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一般的经验法则是在设置东西时首先调用它(就像这里),然后在拆除东西时最后调用它。
The general rule of thumb is to call it first when setting things up (like here) and call it last when tearing things down.
一般来说,您的代码应该在调用 super 之后。一个明显的例外是 dealloc,在这种情况下,您需要在清理完之后调用
[super dealloc]
。In general, your code should go after the call to super. The one obvious exception is dealloc, in which case you want to call
[super dealloc]
after you've cleaned up after yourself.这取决于具体情况,实际上:
对于初始化/清理,显然,由于子类依赖于其超类状态,因此它应该在之后初始化,在之前清理。
一般来说,您很可能需要在超级调用之前和之后添加行为,甚至完全省略超级调用(毕竟,这就是方法重写的用途)。
一般来说
在这种情况下,请参阅其他答案;但由于
viewDidAppear:
是一种类似通知的方法,因此它实际上取决于您的代码是否需要完全初始化的对象,或者参与初始化本身,因此只能在完成后继续进行超级调用。It depends on the specific case, really:
For initialization / cleanup, obviously, since the subclass depends on its superclass state, it should initialize after and cleanup before.
In general, you might very well need to add behavior both before and after the super-call, or even omit the super-call entirely (that's what method override is for, after all).
In this precise case, see the other answers; but since
viewDidAppear:
is a notification-like method, it really depends whether your code needs a fully initialized object, or takes part in the initialization itself and so must only proceed with the super-call once it's done.这取决于你在做什么。您能否提供一些有关您正在使用的对象的背景信息?
例如,在对象销毁的上下文中,您最后调用 super 。
}
It depends what you're doing. Can you provide some context around the objects you're using?
For example, in the context of object destruction , you call super last.
}