Runtime

2020-01-20  本文已影响0人  柱000000001

- (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");

}

上一篇下一篇

猜你喜欢

热点阅读