objc_msgSend执行流程

OC中的方法调用,其实都是转换为objc_msgSend函数的调用

objc_msgSend的执行流程可以分为3大阶段

消息发送

动态方法解析

消息转发



///

+(BOOL)resolveInstanceMethod:(SEL)sel

{

  If(sel == @selector(test)){

  Method method = class_getInstanceMethod(self,@selector(other));

class_addMethod(self,sel,method_getImplementation(method),

  method_getTypeEncoding(method));

  return YES;

  }

return [super resolveInstanceMethod:sel];

}

Void other(id self, SEL _cmd)

{

  NSLog(@“%@-%s-%s”,self,sel_getName(_cmd),__func__);

}

+(BOOL)resolveInstanceMethod:(SEL)sel

{

  If(sel == @selector(test)){

  class_addMethod(Self,sel,(IMP)other,@“v@:”);

  return YES;

  }

  return [super resolveInstanceMethod:sel];

}

///

Method 可以理解为等价于struct method_t*


生成NSMethodSignature

NSMethodSignature * signature = [NSMethodSignature signatureWithObjCTypes : “i@:i”];

NSMethodSignature * signature = [[[MJStudent alloc] init] methodSignatureForSelector:@selector(test:)];

你可能感兴趣的:(objc_msgSend执行流程)