iOS 消息转发(Message Forwarding)

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));
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风雨「83」

你的鼓励将是我创作最大的动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值