Objective-C是动态语言,方法在运行时会被动态转为消息发送,即:objc_msgSend(receiver,selector)。
objc向对象发送消息时,runtime会根据isa指针找到对象所属类,从该类及父类方法列表寻找方法运行 如果没有找到便会抛出异常 unrecognized selector sent to xxx, 但是在这之前有三次拯救机会。
1.Method resolution 方法解析处理阶段
如果调用了方法 首先会调用+(BOOL)resolveInstanceMethod:(SEL)sel【对象方法】, +(BOOL)resolveClassMethod:(SEL)sel【类方法】进行判断,如果YES能接收消息,NO就会进行第二步。
+(BOOL)resolveInstanceMethod:(SEL)sel;
+(BOOL)resolveClassMethod:(SEL)sel;
为未实现的方法动态添加实现,method1未实现,为类添加方法实现,将method2的IMP实现添加到method1。
+(BOOL)resolveInstanceMethod:(SEL)sel{
IMP imp = class_getMethodImplementation(self.class, @selector(method2));
class_addMethod(self.class, @selector(method1), imp, "v@:");
return [super resolveInstanceMethod:sel];
}
-(void)method2{
NSLog(@"哈哈哈%s",__func__);
}
2.Fast forwarding 快速转发阶段
如果第一步返回NO,则进入消息转发第二步。
-(id)forwardingTargetForSelector:(SEL)aSelector;
此方法可以将本类不能处理的方法转发给其他对象进行处理
-(id)forwardingTargetForSelector:(SEL)aSelector {
NSLog(@"%s-%@",__func__,NSStringFromSelector(aSelector));
if (aSelector == @selector(method1)) {
return [LGTeacher alloc];
}
return [super forwardingTargetForSelector:aSelector];
}
3.Normal forwarding 常规转发阶段
如果第二返回self或nil,则进入第三步。
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector;
-(void)forwardInvocation:(NSInvocation *)anInvocation;
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
NSLog(@"%s-%@",__func__,NSStringFromSelector(aSelector));
return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}
-(void)forwardInvocation:(NSInvocation *)anInvocation {
LGTeacher *t = [LGTeacher alloc];
if ([self respondsToSelector:anInvocation.selector]) {
[anInvocation invokeWithTarget:self];
}else if ([t respondsToSelector:anInvocation.selector] ) {
[anInvocation invokeWithTarget:t];
}else {
NSLog(@"%s-%@",__func__,NSStringFromSelector(anInvocation.selector));
}
}