美文网首页
消息转发流程

消息转发流程

作者: 木槿WEIXIAO | 来源:发表于2020-10-26 16:22 被阅读0次

    1. 动态方法解析 

    voidsendMessage(idself,SEL_cmd,NSString*msg){

        NSLog(@"啦啦啦");

    }

    + (BOOL)resolveInstanceMethod:(SEL)sel

    {

        NSString*methName =NSStringFromSelector(sel);

        if([methNameisEqualToString:@"sendMessage:"]) {

            returnclass_addMethod(self, sel, (IMP)sendMessage,"v@:@");

        }

        return NO;

    }

        为类添加方法实现

    2.快速转发

    - (id)forwardingTargetForSelector:(SEL)aSelector

    {

        NSString*methName =NSStringFromSelector(aSelector);

        if([methNameisEqualToString:@"sendMessage:"]) {

            return[PersonalTwonew];

        }

        return [super forwardingTargetForSelector:aSelector];

    }

    找到一个实现该方法的类来调用该方法

    3.慢速转发

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

    {

        NSString*methName =NSStringFromSelector(aSelector);

        if([methNameisEqualToString:@"sendMessage:"]) {

            return [NSMethodSignature signatureWithObjCTypes:"v@:@"];

        }else{

            return [super methodSignatureForSelector:aSelector];

        }

    }

    - (void)forwardInvocation:(NSInvocation*)anInvocation

    {

        SELsel = [anInvocationselector];

        PersonalTwo*personalTwo = [PersonalTwonew];

        if([personalTworespondsToSelector:sel]) {

    //        [anInvocation invokeWithTarget:personalTwo];

        }else{

            [superforwardInvocation:anInvocation];

        }

    }

    分为两点, 第一点:返回方法签名  第二点:判断指定类是否有该方法实现,如果有则指向该方法

    相关文章

      网友评论

          本文标题:消息转发流程

          本文链接:https://www.haomeiwen.com/subject/esoamktx.html