使用objc runtime实现懒加载git
地址:AutoPropertyCocoagithub
本文所指懒加载形式以下数组
- (id)lazyloadProperty{ if(_lazyloadProperty == nil){ _lazyloadProperty = [XClass ...]; } return _lazyloadProperty; }
通常使用宏定义能够轻松完成。可是没有一致性,移植差。安全
利用objc runtime的动态性实现懒加载能够实现即可增长又可删除功能,也能够避免污染类型。该三方弥补了目前没有闭环实现懒加载三方的空缺。框架
主要实现内容:函数
难点:ui
本身实现method swizzlingthis
咱们再实现method swizzling时的两个APIspa
OBJC_EXPORT IMP _Nullable class_replaceMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types) OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0); OBJC_EXPORT void method_exchangeImplementations(Method _Nonnull m1, Method _Nonnull m2) OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
无论使用哪一种,若是这个类型没有实现该方法而是父类实现的话,就须要动态增长一个方法。动态增长的方法在Objc1时代,是能够经过下列方法删除的:线程
OBJC_EXPORT void class_removeMethods(Class _Nullable, struct objc_method_list * _Nonnull) OBJC2_UNAVAILABLE;
Objc2时代以后runtime被重写后没有该方法了,而且新的runtime的类结构看起来就没打算让开发者删除方法,因此这里将过程记下。
首先看类读写器的结构class_rw_t
struct class_rw_t { // Be warned that Symbolication knows the layout of this structure. uint32_t flags; uint32_t version; const class_ro_t *ro; method_array_t methods;//删除这里的一个方法 property_array_t properties; protocol_array_t protocols; Class firstSubclass; Class nextSiblingClass; char *demangledName; #if SUPPORT_INDEXED_ISA uint32_t index; #endif };
method_array_t继承于list_array_tt<method_t, method_list_t>,它是数组结构。存储的内容是method_list_t.
method_list_t又继承于entsize_list_tt<method_t, method_list_t, 0x3>,他也是数组结构。
整个method_array_t结构是二维数组。每次删掉一个method_t须要用新method_list_t替换原对象。
而后是线程安全的问题,须要获取到苹果在操做类型的时候使用的读写锁(pthread_rw_lock_t runtimelock)。没有这把锁任何对runtime的修改都是不可靠的。
最终采起的方式是:劫持暴露了符号的系统函数而后阻塞线程
劫持系统C函数使用的是脸书的鱼钩,这个钩子在macOS其实也是能够正常工做的。
剩下的就是寻找合适的函数了,这函数要知足两个条件:
找了半天发现最合适的只有objc_allocateProtocol()了,objc_allocateProtocol内部会调用calloc(),因此第二个被劫持函数就是calloc。为了减少calloc的开销,须要稍微作一些工做。
虽然不是什么吸引人的UI框架仍是但愿你们点个赞吧。另外,有成都的公司招iOS吗😂。
Email : meterwhite@outlook.com