为什么你希望一个类私下遵守协议?

发布于 2024-12-17 12:44:21 字数 442 浏览 2 评论 0原文

我一直在查看 Apple 的 MVCNetworking 示例项目 AppDelegate 的部分接口定义让我感到困惑。在 .h 文件中,我们有这样的:

@interface AppDelegate : NSObject
{
  ...

但在 .m 文件中,我们有这样的:

@interface AppDelegate () <SetupViewControllerDelegate>
  ...

所以这个类私有符合协议。但为什么要这样做而不是在标头中公开声明呢?

I've been looking at Apple's MVCNetworking example project and part of the interface definition for AppDelegate is puzzling me. In the .h file we have this:

@interface AppDelegate : NSObject
{
  ...

But in the .m file we have this:

@interface AppDelegate () <SetupViewControllerDelegate>
  ...

So this class is privately conforming to the protocol. But why would you want to do this instead of publicly declaring it in the header?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

小姐丶请自重 2024-12-24 12:44:21

一般来说,你应该尽可能少地公开曝光。当AppDelegate 呈现SetupViewController 时,可能会使用AppDelegate 可以是SetupViewController 的委托这一事实。任何其他类都不应该将 AppDelegate 设置为其他一些 SetupViewController 的委托,因此公开宣传这种一致性是没有意义的。

In general, you should publicly expose as little as possible. The fact that the AppDelegate can be a SetupViewController's delegate is probably used when the AppDelegate presents a SetupViewController. No other class should be setting the AppDelegate as some other SetupViewController's delegate, so it wouldn't make sense to publicly advertise that conformance.

三人与歌 2024-12-24 12:44:21

看起来该实现在其“私有”方法之一presentSetupViewControllerAnimated:内部使用了SetupViewController。由于视图控制器不可公开访问(通过属性或其他方式),因此无需从公共角度将类声明为符合协议。换句话说,协议仅与类的实现相关,而不与它所呈现的公共接口相关。

It looks like the implementation uses a SetupViewController internally in one of its "private" methods presentSetupViewControllerAnimated:. Since the view controller is not publicly accessible (through a property or otherwise), there's no need to declare the class as conforming to the protocol from the public point of view. In other words, the protocol is related only to the implementation of the class, and not to the public interface that it presents.

对不⑦ 2024-12-24 12:44:21

有时您想成为另一个对象的委托,但这样做时您可能会收到编译器警告,因为您没有明确声明您的类符合协议所需的方法。正如其他人提到的,面向对象编程的支柱之一是信息隐藏。在标头中声明一个类实现特定协议是不可取的,因为这会违反该原则。它还使您的班级容易被滥用或以非预期的方式使用,因为它使其他班级知道该信息。通过在 .m 文件中声明一个私有类别并让编译器知道您实现此协议的意图,您不仅可以消除可能出现的警告,而且实际上可以使您的代码自我记录。

There are times where you want to be a delegate for another object, but in so doing you may get compiler warnings because you aren't explicitly declaring that your class conforms to the required methods of the protocol. As others have mentioned, one of the pillars of Object Oriented programming is information hiding. It is not desirable to declare in your header that a class implements a particular protocol because you would be breaking that principle. It also opens your class to abuse or to be used in ways it was not intended because it is making that information known to other classes. By declaring a private category in the .m file and letting the compiler know of your intention to implement this protocol, you not only get rid of the warnings that may crop up, but you are, in effect, making your code self-documenting.

云裳 2024-12-24 12:44:21

也许是因为您不想让除了您自己之外的任何人知道您的协议。因此,AppDelegate 外部的任何人都不会将 Appdelegate 的实例作为委托传递给另一个类实例。所以你将能够在内部传递它。

Maybe because you don't want anybody to know about your protocol with except yourself. So no somebody externally of AppDelegate will pass instance of Appdelegate as delegate to another class instance. So you will able to pass it as this internally.

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