美文网首页
Runtime 消息传递

Runtime 消息传递

作者: Brucezhang1 | 来源:发表于2017-04-28 08:15 被阅读8次

*main.m

PersonA *personA = [[PersonA alloc]init];
[personA speechJapan];
  • PersonB.h
- (void)speechJapan;
  • PersonB.m

- (void)speechJapan{
    NSLog(@"speechJapan");
}
  • PersonA.h
- (void)speechEnglish;
- (void)speechJapan;
  • PersonA.m
- (void)speechEnglish{
    NSLog(@"speechEnglist");
}

// 此方法不是方法的实现,需要进行转化
- (void)speechNewLan{
     NSLog(@"speechNewLan");
}

// 将方法转换成方法的实现IMP
IMP convertToIMP(){
    return  class_getMethodImplementation([PersonA class], @selector(speechNewLan));
}
/* 当未找到speechJapan时,首先调用此方法,使有机会添加一个实现的方法
 如果添加了方法并且放回了Yes,则运行时系统会启用一次消息发送的过程。如果返回为NO,
 运行时系统则进入到下一步,消息转发(Message Forwarding)
 */

// 消息派发
+ (BOOL)resolveInstanceMethod:(SEL)sel{
    if (sel == @selector(speechJapan)) {
        class_addMethod(self, sel, convertToIMP(), "");
        return YES;
    }
    return NO;
}


/*实现此方法,运行时系统就会调用次方法,将消息转发给其他指定的对象,
 比如转发给PersonB的对象
 
注:只要返回的对象不是nil或self,消息发送的整个过程就会启动
 发送的对象变为指定返回的对象,比如PersonB。如果返回的是nil或self,则进行下一步转发操作
 */
- (id)forwardingTargetForSelector:(SEL)aSelector{
    if (aSelector == @selector(speechJapan)) {
        PersonB *pB = [[PersonB alloc] init];
        return pB;
    }
    return [super forwardingTargetForSelector:aSelector];
}

// 消息转发第二步 Mormal forwrading

/* 获取方法签名
 调用此方法获取函数的参数和返会类型,如果此方法返回nil
 Runtime则会发出doseNotRecognizeSelector: 消息挂掉程序
 如果返回一个函数签名,Runtime则会创建一个NSINvocation对象,调用ForwradInvacation:
 发送消息给对象
 */
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    
    NSMethodSignature *sign = [[PersonB class] instanceMethodSignatureForSelector:aSelector];
    return [NSMethodSignature signatureWithObjCTypes:"v@:"];
}

// 实现消息的转发
/*
 NSInvocation 实际上是对一个消息的描述,包括selector极其参数等信息
 所以可以在此方法中修改NSInvocation对象,然后调用invokeWithTarget:消息给对象
 */
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    SEL sel = anInvocation.selector;
     PersonB *pB = [[PersonB alloc] init];
    if ([pB respondsToSelector:sel]) {
        [anInvocation invokeWithTarget:pB];
    }else{
        [self doesNotRecognizeSelector:sel];
    }
}

相关文章

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

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

  • runtime底层实现原理

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

  • Runtime 消息传递

    *main.m PersonB.h PersonB.m PersonA.h PersonA.m

  • Runtime消息传递

    Runtime消息传递 一个对象的方法编译器转成消息发送objc_msgSend(obj, foo),Runtim...

  • Runtime 的应用

    前面我们说到:Runtime 消息传递机制Runtime 消息转发机制Runtime 交换方法今天我们来谈谈Run...

  • Runtime梳理(一)消息机制及应用

    Runtime的介绍 Runtime消息的传递和转发 Runtime的应用 1.Runtime的介绍 Object...

  • Objective-C Runtime浅析

    前言 Runtime是什么 Runtime的实现原理消息传递机制Runtime基础数据结构NSObject & i...

  • Runtime 消息传递机制

    求婚时,想给女友买颗好钻戒,那颗他曾参与撰写文案的优雅钻戒,承载着他的所有想象和创造力,很多人被他的文案打动,而他...

  • Runtime消息传递总结

    iOS中类找不到方法时消息处理机制 iOS~runtime理解 一、正常的消息传递 在C等语言中,调用一个方法就是...

  • Runtime(一)消息传递

    要用runtime进行消息传递,首先要导入底层框架,因为框架是包含

网友评论

      本文标题:Runtime 消息传递

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