ios - 为什么 Apple 在 iOS 运行时的类类型结构中对方法列表使用双指针

标签 ios objective-c pointers runtime

我读到在 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/

相关文章:

ios - 带有 Wrap 的 SwiftUI HStack

ios - 提交时出错 - 可执行文件必须包含对armv6的支持

c++ - 用相同的数据填充数组对象还是使用指针?

iphone - 加快图像加载速度

ios - 使用 ShareKit 登录 Facebook 并使用登录凭据查看页面?

c - 将指针作为参数传递给结构的优点?

c++ - 在 C++ 中是否有等同于 `instancetype` 的东西?

iphone - 核心数据删除对象 : not working?

iOS 删除 MKMapView 叠加层不起作用

弹出键盘时 iOS 11 无限滚动