Runtime

2017-09-06  本文已影响0人  琦均煞Sylar

isa 指针

isa 是一个 Class 类型的指针。
实例中:每个实例对象有一个 isa 指针,它指向实例对象的类。
类中:Class 里也有一个 isa 的指针,指向 meteClass(元类)。
元类:元类保存类的方法列表。当类方法被调用时,先回从本身类查找方法的实现。如果没有,元类会像其父类查找该方法。同时注意的是:元类也是类,它也是对象。元类也有 isa 指针,它的 isa 指针最终指向的是一个根元类。根元类的 isa 指针指向本身,它形成了一个封闭的内循环。

Runtime 的数据结构

  struct objc_object{
        Class isa;
  };
    struct objc_class{
        Class isa;
    }
    struct objc_class{
        Class isa;
    }
    Class isa;
    NSString *name;
    NSUinteger age;

Runtime 的消息机制

    [obj xxx];  编译时---> objc_msgSend(obj,"xxx");

函数的定义

由此,可以看出以 objc_ 开头的方法是 runtime 最终的管家,可以获取内存中,类的加载信息列表、关联对象、和关联属性等操作。

字典转模型

有些场景中,模型的属性与服务器端回传的参数一致(服务器字段定义比较规范的)


- (instancetype)initWithDictionary:(NSDidctionary *)dictionary{
   if (dictionary != nil){
        unsigned int count = 0;
        // 获取属性列表
        Ivar *ivarList = class_copyIvarList(self.class, &count);
        // 遍历
        for (int i = 0; i < count; i++){
           Ivar theVar = ivarList[i];
           // 取出每个元素的 属性名字,转换成字典中的 Key
           const char *propertyName = ivar_getName(theVar);
           NSString *key = [NSString stringWithUTF8String:propertyName];
           // 获取字典中的对应属性的值,由于属性是由下划线开头,所以去掉下划线
           id value = [dictionary objectForKey:[key stringByReplacingOccurrencesOfString:@"_" withString:@""]];
           // 为对应属性赋值
           object_setIvar(self.class,theVar,value);
        }
   }
    return self;
}

交换方法 or 动态添加方法

方法交换的实现一般写在类的 load 方法里面,而 initialize 有可能调用多次

//创建一个 category,在 load 方法中
+(void)load{
    Method originalMethod = class_getInstanceMethod(self.class,@selector(navigationBar:shouldPopItem:));
    Method newMethod = class_getInstanceMethod(self.class,@selector(xxx_navigationBar:shouldPopItem:));
    
    BOOL didAddMethod = class_addMethod()
}

属性关联

未完待续

上一篇 下一篇

猜你喜欢

热点阅读