Runtime

作者: 柱000000001 | 来源:发表于2020-01-20 10:45 被阅读0次

    - (void)test

    {

        NSLog(@"test");

    }

    - (void)other

    {

        NSLog(@"other");

    }

    //动态解析

    + (BOOL)resolveInstanceMethod:(SEL)sel

    {

        if(sel ==@selector(test))

        {

            Methodmethod =class_getInstanceMethod(self,@selector(other));

            class_addMethod(self, sel,

                            method_getImplementation(method),

                            method_getTypeEncoding(method));

            returnYES;

        }

        return [super resolveInstanceMethod:sel];

    }

        //或

    + (BOOL)resolveClassMethod:(SEL)sel

    {

        if(sel ==@selector(test))

        {

            Methodmethod =class_getInstanceMethod(self,@selector(other));

            class_addMethod(self, sel,

                            method_getImplementation(method),

                            method_getTypeEncoding(method));

            returnYES;

        }

        return [super resolveClassMethod:sel];

    }

    //消息转发1 Target

    - (id)forwardingTargetForSelector:(SEL)aSelector

    {

        if(aSelector ==@selector(test))

        {

            return[[NSObjectalloc]init];

        }

        return [super forwardingTargetForSelector:aSelector];

    }

    //消息转发2 方法签名

    - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector

    {

        if(aSelector ==@selector(test))

        {

    //        return [NSMethodSignature signatureWithObjCTypes:"v16@0:8"];

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

            return [NSMethodSignature methodSignatureForSelector:aSelector];

        }

        return [super methodSignatureForSelector:aSelector];

    }

    //消息转发3 方法调用

    - (void)forwardInvocation:(NSInvocation*)anInvocation

    {

    //    int a;

    //    [anInvocation getArgument:&a atIndex:2];

        [anInvocationinvokeWithTarget:[[NSObjectalloc]init]];

        NSLog(@"123");

    }

    相关文章

      网友评论

          本文标题:Runtime

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