我读到在 iOS 运行时,类变成结构(其他 oc 对象也是),如下所示:
struct objc_class {
Class isa OBJC_ISA_AVAILABILITY;
#if !__OBJC2__
Class super_class OBJC2_UNAVAILABLE;
const char *name OBJC2_UNAVAILABLE;
long version OBJC2_UNAVAILABLE;
long info OBJC2_UNAVAILABLE;
long instance_size OBJC2_UNAVAILABLE;
struct objc_ivar_list *ivars OBJC2_UNAVAILABLE;
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
struct objc_cache *cache OBJC2_UNAVAILABLE;
struct objc_protocol_list *protocols OBJC2_UNAVAILABLE;
#endif
} OBJC2_UNAVAILABLE;
在上面的结构中,有这样的内容:
struct objc_method_list **methodLists OBJC2_UNAVAILABLE;
我是C
菜鸟,不太明白为什么OC要用指向指针**
的指针来做方法列表。
为什么它不使用与缓存相同的结构:
struct objc_cache *cache OBJC2_UNAVAILABLE;
因为缓存也包含了之前使用过的方法。
我认为Apple使用双指针**
作为方法列表
,而不是单个*
作为缓存
或协议(protocol)
是有原因的。
为什么叫 methodLists
而不是 methodList
?
我现在正在将缓存
、协议(protocol)
与methodLists
进行比较。
我认为出于技术原因,Apple 使用 **
作为方法列表,而不是 *
作为其他方法列表,我想知道。
如果一个指针 *
足以用于方法列表,为什么 Apple 还要使用双**
?
最佳答案
类似问题:Whats is methodLists attribute of the structure objc_class for?
根据上面的帖子和这个article , Apple 之所以将methodLists
设计为双指针(OBJC2.0
也是如此)的原因是,简而言之,双指针结构对于添加 来说更快category
方法在运行时进入类的方法列表。
由于双指针是针对一列方法列表的,所以考虑到每个类别都有一个方法列表,在运行时向类中添加一个或多个类别时,不需要一个一个地添加方法,只需将指针添加到类别方法列表到类 methodLists
,它有一个指向每个列表/数组指针的指针。
我不确定我是否解释得足够清楚。
关于ios - 为什么 Apple 在 iOS 运行时的类类型结构中对方法列表使用双指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38559105/