Objective-C Runtime 是一个运行时库。它可以在程序运行时改变程序的结构如:添加属性、添加方法、交换方法等。

每个对象都有个 isa 属性指向对象所属类;有个 super_class 属性指向所属类的父类;
类也是对象,它的 isa 指向类的元类;类的元类的父类等于类的父类的元类。
实例的属性存储在实例中,实例的方法存储在所属类中,类方法存储在元类中。
类中存放着实例方法列表,在这个方法列表中 SEL 作为 key,IMP 作为 value。

1.检测 SEL 是否应该被忽略。
2.检测发送的 target 是否为 nil ,如果是则忽略该消息。
3.当调用实例方法时,通过 isa 指针找到实例对应的 class 并且在其中的缓存方法列表以及方法列表中进行查询,如果找不到则根据 super_class 指针在父类中查询,直至根类(NSObject 或 NSProxy)。
当调用类方法时,通过 isa 指针找到实例对应的 metaclass 并且在其中的缓存方法列表以及方法列表中进行查询,如果找不到则根据 super_class 指针在父类中查询,直至根类(NSObject 或 NSProxy)。

// 在分类的 .h 文件中声明“属性”

  1. @property (nonatomic) NSInteger age;

// 在分类的 .m 实现以下两个方法

  1. - (void)setAge:(NSInteger)age{
  2. // 使用运行时关联对象,Person对象self强引用NSNumber对象@(age),并且设置标记为"age"(可以根据该标记来获取引用的对象age,标记可以为任意字符,只要setter和getter中的标记一致就可以)
  3. // 参数1:源对象
  4. // 参数2:关联时用来标记属性的key(因为可能要添加很多属性)
  5. // 参数3:关联的对象
  6. // 参数4:关联策略。assign,retain,copy对应的枚举值
  7. objc_setAssociatedObject(self, "age", @(age), OBJC_ASSOCIATION_COPY_NONATOMIC);
  8. }
  9. - (NSInteger)age{
  10. // 根据"age"标识取Person对象self强引用的NSNumber对象@(age)
  11. // 参数1:源对象
  12. // 参数2:关联时用来标记属性的key(因为可能要添加很多属性)
  13. return [objc_getAssociatedObject(self, "age") integerValue];
  14. }
  1. + (void)load {
  2. static dispatch_once_t onceToken;
  3. dispatch_once(&onceToken, ^{
  4. Method originMethod = class_getInstanceMethod([self class], @selector(originFunction));
  5. Method customMethod = class_getInstanceMethod([self class], @selector(customFunction));
  6. // 尝试自定义方法实现添加到系统方法中
  7. BOOL addSuccess = class_addMethod([self class], @selector(originFunction), method_getImplementation(customMethod), method_getTypeEncoding(customMethod));
  8. if (addSuccess) {
  9. // 添加成功后,系统方法实现设置为自定义方法中
  10. class_replaceMethod([self class], @selector(customFunction), method_getImplementation(originMethod), method_getTypeEncoding(originMethod));
  11. } else {
  12. method_exchangeImplementations(originMethod, customMethod);
  13. }
  14. });
  15. }
  1. Method targetMethod = class_getInstanceMethod([aTarget class], aSelector);
  2. if (!class_addMethod([BBTimerManager class], aSelector, method_getImplementation(targetMethod), method_getTypeEncoding(targetMethod))) {
  3. class_replaceMethod([BBTimerManager class], aSelector, method_getImplementation(targetMethod), method_getTypeEncoding(targetMethod));
  4. }
  1. - (void)encodeWithCoder:(NSCoder *)encoder {
  2. unsigned int count = 0;
  3. Ivar *ivars = class_copyIvarList([Movie class], &count);
  4. for (int i = 0; i < count; i++) {
  5. // 取出 i 位置对应的成员变量
  6. Ivar ivar = ivars[I];
  7. // 查看成员变量
  8. const char *name = ivar_getName(ivar);
  9. NSString *key = [NSString stringWithUTF8String:name];
  10. id value = [self valueForKey:key];
  11. // 归档
  12. [encoder encodeObject:value forKey:key];
  13. }
  14. free(ivars);
  15. }
  16. - (id)initWithCoder:(NSCoder *)decoder {
  17. if (self = [super init]) {
  18. unsigned int count = 0;
  19. Ivar *ivars = class_copyIvarList([Movie class], &count);
  20. for (int i = 0; i < count; i++) {
  21. // 取出i位置对应的成员变量
  22. Ivar ivar = ivars[I];
  23. // 查看成员变量
  24. const char *name = ivar_getName(ivar);
  25. NSString *key = [NSString stringWithUTF8String:name];
  26. id value = [decoder decodeObjectForKey:key];
  27. // 设置到成员变量身上
  28. [self setValue:value forKey:key];
  29. }
  30. free(ivars);
  31. }
  32. return self;
  33. }

版权声明:本文为ebamboo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/ebamboo/p/14285440.html