结构体 objc_class 的 methodLists 属性有什么用?
查看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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我对 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 :)
objc-class.m,由 Georg 链接:
因此,简短的回答是“因为类别”。当注入一个类别时,不是尝试将其方法列表与现有列表合并,而是简单地将一个新条目添加到
methodLists
中,并将其设置为来自该类别的列表。这可能会使类别注入更快,因为它避免了(潜在的)大量重新分配和复制。The purpose is explained in objc-class.m, as linked by Georg:
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.