结构体 objc_class 的 methodLists 属性有什么用?

发布于 2024-12-27 01:52:48 字数 266 浏览 1 评论 0原文

查看runtime.h内部,我找到了结构体objc_class的定义。

在各种成员中,我们有这个:-

struct objc_method_list **methodLists

我们肯定需要知道一个类具有哪些所有方法, 但是方法列表应该没问题,但是为什么我们有“列表”呢?

为什么不只列出一份清单?

另外,任何人都可以指定,方法是从该列表的超类部分继承的,还是我们通过指向父类结构的超类指针来获取它们。

Looking inside the runtime.h, I found the definition of the structure objc_class.

Among various members, We have this :-

struct objc_method_list **methodLists

We definitely need to know what all methods a class has,
But a list of methods should be fine, but why do we have "lists" ?

Why not just one list ?

Also, can anyone specify that, Are methods inherited from superclass part of that list or we get to them via superclass pointer that points to parent class's structure.

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

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

发布评论

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

评论(2

夏夜暖风 2025-01-03 01:52:48

这是我对 struct objc_method_list **methodLists 的详细调查: http://blog.csdn.net /jasonblog/article/details/7303618

简而言之,methodLists 默认存储实例方法的 SEL-IMP 映射。在这种情况下,它只有一个列表。

正如名称“methodLists”所示,它可以包含多个列表。如果你给一个类添加一个类别,运行时系统会在methodLists中再插入一个列表,它指向该类别的方法列表。

几个月前我试图回答这个问题,但当时由于网络问题放弃了我的答案。现在我又遇到了:)

Here is my detail investigation into struct objc_method_list **methodLists : http://blog.csdn.net/jasonblog/article/details/7303618

And in short, methodLists stores SEL-IMP mapping of the instance methods by default. In this situation, it has only one list.

As the name 'methodLists' suggests, it can contain more than one list. If you add a category to a class, the runtime system will insert one more list into methodLists, which points to the method-list of the category.

I tried to answer this question several months ago, but at that time SO discard my answer due to network problem. Now I meet it again :)

温柔少女心 2025-01-03 01:52:48

objc-class.m,由 Georg 链接:

cls->methodLists 可以采用以下三种形式之一:

  1. NULL:该类没有方法。
  2. 非 NULL,设置了 CLS_NO_METHOD_ARRAY:cls->methodLists 点
    到单个方法列表,这是类的唯一方法列表。
  3. 非 NULL,CLS_NO_METHOD_ARRAY 清除:cls->methodLists 指向
    方法列表指针的数组。数组块的末尾
    设置为-1。如果实际的方法列表数量较少
    除此之外,数组的其余部分为 NULL。

附加类别以及添加和删除类别可能会发生变化
班级名单的形式。另外,单独的方法列表
修复后可能会重新分配。

类最初被读取为#1 或#2。如果附加了类别
或者添加其他方法,该类更改为#3。一旦进入表格#3,
即使删除了方法,该类也永远不会降级为 #1 或 #2。
使用 objc_addClass 添加的类最初是#1 或#3。

因此,简短的回答是“因为类别”。当注入一个类别时,不是尝试将其方法列表与现有列表合并,而是简单地将一个新条目添加到 methodLists 中,并将其设置为来自该类别的列表。这可能会使类别注入更快,因为它避免了(潜在的)大量重新分配和复制。

The purpose is explained in objc-class.m, as linked by Georg:

cls->methodLists may be in one of three forms:

  1. NULL: The class has no methods.
  2. non-NULL, with CLS_NO_METHOD_ARRAY set: cls->methodLists points
    to a single method list, which is the class's only method list.
  3. non-NULL, with CLS_NO_METHOD_ARRAY clear: cls->methodLists points to
    an array of method list pointers. The end of the array's block
    is set to -1. If the actual number of method lists is smaller
    than that, the rest of the array is NULL.

Attaching categories and adding and removing classes may change
the form of the class list. In addition, individual method lists
may be reallocated when fixed up.

Classes are initially read as #1 or #2. If a category is attached
or other methods added, the class is changed to #3. Once in form #3,
the class is never downgraded to #1 or #2, even if methods are removed.
Classes added with objc_addClass are initially either #1 or #3.

The short answer is therefore "because of categories." When a category is injected, rather than try to combine its method list with the one existing list, a new entry is simply added to methodLists, and set to the list coming from the category. This probably makes category injection faster, since it avoids (potential) large reallocations and copying.

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