记录下开发中遇到的问题

2020-03-02  本文已影响0人  SinceMeYou

1.利用UITableView+FDTemplateLayoutCell 和 Masonry 实现cell高度的自动计算

2. 对Swizzling中方法的理解

替换实例方法的
+(void)swizzlingInstanceMethodWithOriginSelector:(SEL)originSelector  swizlingSelector:(SEL)swizlingSelector{
    Method originMethod = class_getInstanceMethod (self, originSelector);
     Method swizlingMethod = class_getInstanceMethod(self, swizlingSelector);
    ///此处判断添加的方法是否在原类中存在,不存在的话会添加成功
    ///意思就是,在当前的class中添加swizzle方法,方法名为originSelector,如果可以添加成功,说明swizzle方法的方法名在其父类中存在,不成功说明当前swizzle方法在其父类中不存在
     BOOL addMethod =class_addMethod(self, originSelector, method_getImplementation(swizlingMethod), method_getTypeEncoding(swizlingMethod));
     if(addMethod){ ///可以添加成功,将原有方法的方法名字替换为swizlingSelector
    class_replaceMethod(self, swizlingSelector, method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
    }else{ //不成功的话交互两个方法即可
         method_exchangeImplementations(originMethod, swizlingMethod);
    }
    }
替换类方法的
+(void)swizzlingClassMethodWithOriginSelector:(SEL)originSelector  swizlingSelector:(SEL)swizlingSelector{
Method originMethod = class_getClassMethod(self, originSelector);
Method swizlingMethod = class_getClassMethod(self, swizlingSelector);
Class mateClass = objc_getMetaClass(class_getName([self class]));
BOOL add =class_addMethod(mateClass, originSelector,method_getImplementation(swizlingMethod) , method_getTypeEncoding(swizlingMethod));
if(add){
    class_replaceMethod(mateClass, swizlingSelector, method_getImplementation(originMethod) , method_getTypeEncoding(originMethod));
}else{
    method_exchangeImplementations(originMethod, swizlingMethod);
}
}
上一篇 下一篇

猜你喜欢

热点阅读