美文网首页
runtime (二)消息转发

runtime (二)消息转发

作者: 看谷秀 | 来源:发表于2019-03-26 10:36 被阅读0次

目录
一 消息转发
概述: 什么意思呢 声明了一个方法,但没有实现,就会触发消息转发,
例如:

   [[Animal new] eat]; // 调用Animal声明的eat方法,但是没有实现

又或者

   id test = [[[self class] alloc] init];
    [test substringFromIndex:0];//你调用字符串声明的substr...方法,但是控制器没有实现

这就崩溃了

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController substringFromIndex:]: unrecognized selector sent to instance 0x7f995f80a580'
无法识别发送实例(地址为0x7f995f80a580) 的选择器 substringFromIndex

上代码


#import "Animal.h"

#import <objc/message.h>
#import "AnimalForwardingTarget.h"
#import "AnimalForwardInvocation.h"
@implementation Animal

/*再头文件中声明了eat 函数,但是没有实现这个方法,所以有一条警告⚠️:Method definition for 'eat' not found */


/**在发生崩溃之前,会做三步处理**/
/**/

/*动态方法解析*/
/*在本类 Class Animal中寻找可以处理“eat”的方法*/

/*
 * function: resolveInstanceMethod
 * NSObject.h中声明的类方法 提供给用户向该类的动态添加selector(函数方法)的机会
 *  备注: 当调用方法没有找到实现 会调用这个方法  仅限实例方法
 *  扩展:如果 使用@dynamic 取消编译器自动生成getter/setter方法则需要在这里添加对应方法
 */

+ (BOOL)resolveInstanceMethod:(SEL)sel{   //resolve 解析
    NSLog(@"sel = %@",NSStringFromSelector(sel));
    if (sel == @selector(eat)) {
            class_addMethod(self, sel, (IMP)eatMeat, "v@:@");
            return YES;
        }
    return [super resolveInstanceMethod:sel];
}

/*
 *function: eatMeat
 *通过resolveInstanceMethod 的IMP指向函数,
 */
void eatMeat(id self,SEL sel) {
    NSLog(@"---方法一:动态方法解析 eat meat---");
}

//如果动态方法解析无法处理(比如把上面的 resolveInstanceMethod 函数注释掉)则系统会把进入备援接收者处理该消息
/*
 *function: forwardingTargetForSelector
 *如果resolveInstanceMethod 中并没有处理 eat事件的动态方法,系统将会在进入本函数尝试在其他类中寻找实现选择子为"eat"的方法
 *本例Class Action将作为“备援接收者”处理 eat事件
 */
- (id)forwardingTargetForSelector:(SEL)aSelector{
    NSString *selectorStr = NSStringFromSelector(aSelector);//获取消息的选择子
    if ([selectorStr isEqualToString:@"eat"]) {
        AnimalForwardingTarget *action = [AnimalForwardingTarget new];
        return action;
    }
    return [super forwardingTargetForSelector:aSelector];

}

/*如果没有备援接收者处理该消息时,便会进入消息重定向*/
/*调用此方法如果不能处理就会调用父类的方法,一直到NSObject,如果NSObject也无法处理,则会调用doesNotRecognizeSelector抛出异常*/
/*
 消息重定向 第一步:方法签名 参数sel
 获取
 */
- (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
{
    NSString *selectedStr = NSStringFromSelector(aSelector); //获取消息的选择子
    if ([selectedStr isEqualToString:@"eat"]) {
        NSMethodSignature *sign = [NSMethodSignature signatureWithObjCTypes:"v@:@"];
        return sign;
    }
    NSMethodSignature *sign = [super methodSignatureForSelector:aSelector];
    return sign;
}
//修改选择子为“eatDogFood” ,此为AnimalForwardInvocation 中的方法 实现
    //转发 调用
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
    anInvocation.selector = NSSelectorFromString(@"eatDogFood");//新方法
    // 1 设置 
    AnimalForwardInvocation* animal = [[AnimalForwardInvocation alloc] init];//新对象
    
    // 2 如果新对象
    if ([animal respondsToSelector:[anInvocation selector]]) {
        [anInvocation invokeWithTarget:animal];
    } else {
        [super forwardInvocation:anInvocation];
    }
}
/*  ================>>>  牛逼闪闪三大阶段 分析总结  <<<================  */

/*
 一 : 动态方法解析
 + (BOOL)resolveInstanceMethod:(SEL)sel  传来SEL对象
 1 class_addMethod(self, sel, (IMP)eatMeat, "v@:@"); 动态添加方法(注意这里也需要签名) 1谁调用 2调用什么方法 3imp指针找到实现 4实现方法类型
 2 实现 eatMeat 方法
 
 ps: + (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
     如果调用的是类方法,回调用这个方法动态解析, 对象方法则会调用resolveInstanceMethod方法
 
 class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, const char * _Nullable types)
 const char * _Nullable types:会用两个预留参数 0:self 1:selector
 "v@:@" VS void eatMeat(id self,SEL sel)  ??妈的少一个啊  v@:@: 这个比较科学啊
 
 
*/


/*
 二 :消息重定义
 - (id)forwardingTargetForSelector:(SEL)aSelector
    注意返回的id ,返回备源接受者 创建一个对象给他返回,让他去这个对象里面找 传过来的SEL方法
 
    ps: 需要在这个对象里面 先声明及其实现 ,这里eat的方法声明及实现
 */

/*
 三 : 完整消息转发 总结
  本质是通过NSIvocation 进行消息发送 两个方法,一个传入sel对象返回签名,第二个生成NSInvocation对象 发送消息
 
 1  - (NSMethodSignature*)methodSignatureForSelector:(SEL)aSelector
     传来selector对象 给其方法签名
 2  - (void)forwardInvocation:(NSInvocation *)anInvocation
      将签名的方法封装为NSIvocation对象传过来, 设置invocation对象新的执行方法 绑定执行者及调用
 */

/* 引申 */
/*
 ios 三种调用方法
  1 直接调用== obj_msgsend
  2 perform
  3 NSIvocation调用
 */

NSInvocation

- (void)learnInvocation
{
    //    NSInvocation 使用
    //
    //    一 包装对象 (需要签名)
    //    二 设置调用者 ps:1设置方法名 2设置参数 (切记index 从2开始 预留1 self 2 selector)
    //    三 调用
    //    四 设置变量保存返回值
    
    NSMethodSignature *singature = [NSMethodSignature signatureWithObjCTypes:"@@:@"];
    //    NSInteger argumentNum = singature.numberOfArguments;//打印参数个数
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:singature];
    //设置方法 设置响应者
    invocation.selector = @selector(invocationImp:);
    invocation.target = self;
    
    //设置参数
    NSString *str = @"124";
    [invocation setArgument:&str atIndex:2];
    //设置返回
    id returnValue = nil;
    [invocation getReturnValue:&returnValue];
    
    [invocation invoke];// invoke 引用
//    self.superclass

demo
https://github.com/mythBoy/megforword.git

相关链接
https://www.jianshu.com/p/672c0d4f435a//NSInvocation

https://blog.csdn.net/qq_27909209/article/details/74560358//消息转发

相关文章

  • runtime底层实现原理

    一、Runtime介绍二、Runtime源码初探三、Runtime消息传递四、Runtime消息转发五、Runti...

  • runtime (二)消息转发

    目录一 消息转发概述: 什么意思呢 声明了一个方法,但没有实现,就会触发消息转发,例如: 又或者 这就崩溃了 上...

  • Effective Objective-C读后笔记(2)

    11、runtime消息转发机制 runtime的消息转发流程图消息转发 消息转发的示例实现 这里也给大家推荐一篇...

  • iOS - Runtime - 概念和方法交换

    runtime的概述runtime的相关概念runtime消息机制消息传递动态方法解析消息转发runtime的作用...

  • 消息转发机制原理?

    iOS runtime探究(二): 从runtime开始深入理解OC消息转发机制https://www.jians...

  • iOS runtime(二)消息转发

    当调用[recever message]时,其实执行的是id objc_msgSend(id self, SEL ...

  • runtime 消息转发

    实例方法的消息传递:消息的转发建立在objc_msgSend(id, SEL, ...)来实现的。首先会在类对象的...

  • Runtime 消息转发

    目录 消息转发背景知识 消息转发使用方式 消息转发常见问题 消息转发背景知识 1.消息转发的定义Objective...

  • Runtime消息转发

    我们还是先从实际代码入手吧,首先,我们先新建一个类,就Person类吧,大家刚学OC的时候用的最多的就是Perso...

  • runtime - 消息转发

    通过前边的学习我们知道,某个类或者对象调某个方法实际上就是给这个类/对象发送消息,如果我们某个对象要调用某个方法,...

网友评论

      本文标题:runtime (二)消息转发

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