消息转发流程

2020-10-26  本文已影响0人  木槿WEIXIAO

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];

    }

}

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

上一篇下一篇

猜你喜欢

热点阅读