Objective-C 中的私有方法和受保护方法

发布于 2024-10-14 00:41:09 字数 213 浏览 6 评论 0原文

在 Objective-C 中定义私有方法和受保护方法的推荐方法是什么?一个网站建议在私有方法的实现文件中使用类别,另一个网站建议使用尾随下划线或 XX_,其中 XX 是一些特定于项目的代码。苹果自己用什么?

那么受保护的方法呢?我读到的一种解决方案是在单独的文件中使用类别,例如 CLASS_protected.h 和 CLASS_protected.m,但这似乎会变得非常臃肿。我应该怎么办?

What is the recommended way to define private and protected methods in Objective-C? One website suggested using categories in the implementation file for private methods, another suggested trailing underscores, or XX_ where XX is some project-specific code. What does Apple itself use?

And what about protected methods? One solution I read was to use categories in separate files, for example CLASS_protected.h and CLASS_protected.m but this seems like it could get very bloated. What should I do?

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

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

发布评论

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

评论(2

猫烠⑼条掵仅有一顆心 2024-10-21 00:41:09

存在三个问题:

  1. 对编译器隐藏。

    也就是说,使其他人无法 #import 某些内容并查看您的方法声明。为此,请将您的私有 API 放入单独的头文件中,在 Xcode 中将该标头的角色标记为“私有”,然后将其导入到您需要访问所述私有 API 的项目中。

    使用类别或类扩展来声明附加方法。

  2. 防止碰撞

    如果您要实现大量内部 goop,请使用通用前缀或其他不太可能与 Apple 提供的(或第三方)提供的方法发生冲突的内容。这对于类别尤其重要,而对于现有类的叶节点子类则不太重要。

    发布建议使用下划线的网站链接,因为它们是错误的、错误的、错误的。系统使用前导下划线来标​​记私有 API,您很容易遇到冲突。

  3. 从运行时隐藏。

    别打扰。它只会使调试/崩溃分析变得更加困难,任何有足够决心在运行时捣乱的人都将能够破解您的应用程序。

There are three issues:

  1. Hiding from compiler.

    That is, making it impossible for someone else to #import something and see your method declarations. For that, put your private API into a separate header file, mark that header's role as "Private" in Xcode, and then import it in your project where you need access to said private API.

    Use a category or class extension to declare the additional methods.

  2. Preventing collisions

    If you are implementing lots of internal goop, do so with a common prefix or something that makes a collision with Apple provided (or third party) provided methods exceedingly unlikely. This is especially critical for categories and not nearly as critical for your leaf node subclasses of existing classes.

    Post the link for the site suggesting leading underscores, as they are wrong, wrong, wrong. Leading underscores are used by the system to mark private API and you can run into collisions easily enough.

  3. Hiding from the runtime.

    Don't bother. It just makes debugging / crash analysis harder and anyone determined enough to muck around at the runtime will be able to hack your app anyway.

咆哮 2024-10-21 00:41:09

Objective C 中没有“真正的”私有方法,因为运行时将允许通过记录的公共 API,使用字符串名称访问任何类中的任何方法。

我从不为“私有”方法创建单独的接口文件,并且如果我尝试在文件范围之外使用这些方法中的任何一个,都会让编译器抱怨。

XX_ 似乎是创建伪命名空间的临时方法。这个想法是阅读 Apple 的文档以及您将来可能随时使用的任何框架的文档,并选择其他这些框架都不太可能使用的 XX 前缀。

There are no "real" private methods in Objective C, as the run-time will allow, via documented public APIs, access any method in any class by using their string names.

I never do separate interface files for "private" methods, and let the compiler complain if I try to use these any of these methods outside of file scope.

The XX_ seems to be the ad hoc means to create a pseudo namespace. The idea is to read Apple's docs and the docs of any frameworks you might use at any time in the future, and pick an XX prefix that none of these others is ever likely to use.

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