Category中添加属性名冲突了怎么办

2016-11-01  本文已影响182人  柳爷在深圳

Category的同名方法覆盖并不是真的其他同名方法就消失了,而是因为系统调用方法的时候根据方法名在method_list中查找方法,找 到第一个名字匹配的方法之后就不继续往下找了。所以每次调用的都是method_list中最前面的同名方法。实际其他同名方法还在 method_list中。
所以我们可以根据selector查找到所有的同名method,然后调用

static inline void __invoke_all_method(id self, SEL selecotr) 
{  
    //1. 根据self,获取class Class class = object_getClass(self); 
     //2. 获取方法列表 uint count; Method *methodList = class_copyMethodList(class, &count); 
    //3. 遍历方法列表 for (int i = 0; i < count; i++) 
    {
         Method method = methodList[i];
         //4. 根据SEL查找方法 
        if (!sel_isEqual(selecotr, method_getName(method)))
         { continue; }
         //5. 获取方法的实现
         IMP implement = method_getImplementation(method); 
        //6. 直接调用方法的实现
         ((void(*)(id,SEL))implement)(self, selecotr); 
    } 
} 
+ (void)invokeAllClassMethodWithSelector:(SEL)selector 
{
     __invoke_all_method(self, selector); 
}

根据刚刚介绍的原理,我们封装了一个通过selector调用所有同名method的方法。

在系统的+initialize中,我们用invokeAllClassMethodWithSelector调用自定义 的+categoryInitialize。这时候,在category的+categoryInitialize中添加属性,就不怕Category覆盖了。

上一篇 下一篇

猜你喜欢

热点阅读