ios消息转发机制

动态方法解析:

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消息转发机制)