美文网首页
ios消息转发机制

ios消息转发机制

作者: 奔走的小白杨 | 来源:发表于2021-07-05 10:44 被阅读0次

    动态方法解析:

    void method(id self, SEL _cmd, NSString *mes){

        NSLog(@"实现方法");

    }

    + (BOOL)resolveInstanceMethod:(SEL)sel {

        if ([NSStringFromSelector(sel) isEqualToString:@"method"]) {

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

        }

        return NO;

    }

    快速转发:

    - (id)forwardingTargetForSelector:(SEL)aSelector {

        if ([NSStringFromSelector(aSelector) isEqualToString:@"method"]) {

            //需要转发的类

            return[NewClass new];

        }

        return [super forwardingTargetForSelector:aSelector];

    }

    慢速转发:

    方法签名:

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {

        if ([NSStringFromSelector(aSelector) isEqualToString:@"method"]) {

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

        }

        return [super methodSignatureForSelector:aSelector];

    }

    消息转发:

    - (void)forwardInvocation:(NSInvocation*)anInvocation {

        SELsel = [anInvocationselector];

        NewClass *newClass = [NewClass new];

        if([newClass respondsToSelector:sel]) {

            [anInvocationinvokeWithTarget:newClass];

        }

        [superforwardInvocation:anInvocation];

    }

    - (void)doesNotRecognizeSelector:(SEL)aSelector {

        //防止找不到方法崩溃

        NSLog(@"找不到方法");

    }

    相关文章

      网友评论

          本文标题:ios消息转发机制

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