为什么 SEL 不是 Objective-C 中的一个类?

发布于 2024-08-07 12:44:14 字数 122 浏览 4 评论 0原文

在 Objective-C 中(至少是 Apple 风格的 Obj-C),为什么 SEL 不是一个类?是效率问题吗?是为了防止某种无限递归吗?难道只是没有动力让 SEL 成为一门课程吗?欢迎猜测,但请告诉我答案是历史真相还是猜测。

In Objective-C (at least, the Apple flavor of Obj-C), why is SEL not a class? Is it a matter of efficiency? Is it to prevent some sort of infinite recursion? Was there merely no incentive to make SEL a class? Speculation welcome, but please let me know if an answer is historical truth or speculation.

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

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

发布评论

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

评论(2

小伙你站住 2024-08-14 12:44:14

它可以追溯到 Objective-C 的最早版本。如果我能找到我的 Objective-C 书,我可以给你一个参考,但我认为它的好处并不明显。 Objective-C 的早期版本是 C 语法的非常小的扩展。

在 GCC 中,SEL 类型被实现为 const char *,指向选择器名称的字符串表示形式。此实现利用编译器中已实现的常量字符串合并来确保选择器值的唯一性。


“这本书”当然指的是面向对象编程:一种进化方法。感谢弗里德里希唤起了我的记忆。

It goes back to the earliest versions of Objective-C. If I could find my copy of the Objective-C book, I could give you a reference, but I think it just wasn't obvious what the benefit would be. The early versions of Objective-C were very minimal extensions of the C syntax.

In GCC, the SEL type was implemented as a const char *, pointing to a string representation of the selector's name. This implementation took advantage of constant string coalescing, which was already implemented in the compiler, to ensure uniqueness of selector values.


"The book" of course is referring to Object-Oriented Programming: An Evolutionary Approach. Thanks for jogging my memory Friedrich.

邮友 2024-08-14 12:44:14

使其成为类类型几乎没有任何好处。如果它是一个 NSObject 子类,那么对于实际上应该是唯一标识符的东西来说,这将是巨大的开销。此时分派 Objective-C 调用将导致代码的性能关键部分无法承受的惩罚。

是的,我正在猜测为什么这种类型是这样的。

Making it a class type adds little or no benefit. If it were an NSObject subclass it would be a tremendous amount of overhead for something that is effectively supposed to be a unique identifier. To dispatch an Objective-C call at that point would incur a penalty such a perfomance critical section of code can't afford to pay.

And yes, I'm speculating as to justifications why the type is what it is.

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