Obj-C:调用超类

发布于 2024-10-13 05:11:58 字数 232 浏览 8 评论 0原文

- (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

装纯掩盖桑 2024-10-20 05:11:58

一般的经验法则是在设置东西时首先调用它(就像这里),然后在拆除东西时最后调用它。

The general rule of thumb is to call it first when setting things up (like here) and call it last when tearing things down.

别想她 2024-10-20 05:11:58

一般来说,您的代码应该在调用 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.

花落人断肠 2024-10-20 05:11:58

这取决于具体情况,实际上:

  • 对于初始化/清理,显然,由于子类依赖于其超类状态,因此它应该在之后初始化,在之前清理。

  • 一般来说,您很可能需要在超级调用之前和之后添加行为,甚至完全省略超级调用(毕竟,这就是方法重写的用途)。

    一般来说

在这种情况下,请参阅其他答案;但由于 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.

梨涡 2024-10-20 05:11:58

这取决于你在做什么。您能否提供一些有关您正在使用的对象的背景信息?

例如,在对象销毁的上下文中,您最后调用 super 。

- (void)dealloc {
[someObj release];

[super dealloc];

}

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.

- (void)dealloc {
[someObj release];

[super dealloc];

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文