Preprocess

  在使用forwarding机制前,会先经历2个步骤,只有当这2个步骤均失败的情况下,才会激活forwarding。

1、+(BOOL)resolveInstanceMethod:(SEL)selector、resolveClassMethod。

  当第一次没找到SEL时,调用上述两方法之一。如果在上述2方法中加入了方法,并返回YES,则会尝试重新解析此SEL。

  

2、-(id)forwardingTargetForSelector:(SEL)selector,当上述第1条机制失败后,本条机制会被激活。此方法返回能够处理SEL的对象。如果返回非nil,则会调用此对象的SEL。此种机制无法定制参数。

  当上述2个机制均失败后,会起用forwarding机制,如下。

  

Message Forwarding

  If you send a message to an object that does not handle that message, before announcing an error the runtime sends the object a forwardInvocation: message with an NSInvocation object as its sole argument—the NSInvocation object encapsulates the original message and the arguments that were passed with it.

  forwardInvocation方法可以帮助我们轻松的实现完美转发。

 - (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([someOtherObject respondsToSelector:
[anInvocation selector]])
[anInvocation invokeWithTarget:someOtherObject];
else
[super forwardInvocation:anInvocation];
}

  forwarding实际上提供了一种多生继承的变相实现。返回值被存储在NSInvocation中,NSObject框架会把NSInvocation中的返回值返回给原调用者

  

  Although forwarding mimics inheritance, the NSObject class never confuses the two. Methods like respondsToSelector: and isKindOfClass: look only at the inheritance hierarchy, never at the forwarding chain. If, for example, a Warrior object is asked whether it responds to a negotiate message, 

 if ( [aWarrior respondsToSelector:@selector(negotiate)] )
...

  the answer is NO, even though it can receive negotiate messages without error and respond to them.

  如果想让respondsToSelector对代理的方法也返回YES,可以按如下实现:

 - (BOOL)respondsToSelector:(SEL)aSelector
{
if ( [super respondsToSelector:aSelector] )
return YES;
else {
/* Here, test whether the aSelector message can *
* be forwarded to another object and whether that *
* object can respond to it. Return YES if it can. */
}
return NO;
}

  if an object forwards any remote messages it receives, it should have a version of methodSignatureForSelector: that can return accurate descriptions of the methods that ultimately respond to the forwarded messages

 - (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature) {
signature = [surrogate methodSignatureForSelector:selector];
}
return signature;
}

  

Message Forwarding的更多相关文章

  1. iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...

    本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...

  2. 【引】runtime全解析,P1:Programming Guide

    h2.Overview Objective-C language defers as many decisions as it can from compile time and link time ...

  3. Objective-C Runtime

    原文地址:http://tech.glowing.com/cn/objective-c-runtime/ 原作者:顾鹏 如有侵权,请联系本人删除 Objective-C Objective-C 扩展了 ...

  4. iOS运行时Runtime浅析

    运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...

  5. Effective Objective-C 2.0 — 第12条:理解消息转发机制

    11 条讲解了对象的消息传递机制 12条讲解对象在收到无法解读的消息之后会发生什么,就会启动“消息转发”(message forwarding)机制, 若对象无法响应某个选择子,则进入消息转发流程. ...

  6. iOS开发——高级篇——Objective-C特性:Runtime

    Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和转发.下面通过分析Apple开源的 ...

  7. 李洪强iOS经典面试题上

    李洪强iOS经典面试题上     1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...

  8. 详解Objective-C runtime

    感谢翻译小组成员wingpan热心翻译.本篇文章是我们每周推荐优秀国外的技术类文章的其中一篇.如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315 ...

  9. 招聘一个靠谱的ios

    1. 风格纠错题 修改方法有很多种,现给出一种做示例: 最终改为: 下面对具体修改的地方, 2. 什么情况使用 weak 关键字,相比 assign 有什么不同? 什么情况使用 weak 关键字? 1 ...

随机推荐

  1. source idea of Unit

    After the construction of Global environment setting code, there is a convenient way for us in the f ...

  2. 读写INI辅助类

    using System.Text; using System.Runtime.InteropServices; using System; namespace Benson.INI读写 { #reg ...

  3. S2sh整合MAven项目所需坐标大全

    <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> & ...

  4. 文件IO

    在unix世界中视一切为文件,无论最基本的文本文件还是网络设备或是u盘,在内核看来它们的本质都是一样的.大多数文件IO操作只需要用到5个函数:open . read . write . lseek 以 ...

  5. 【英语】Bingo口语笔记(3) - 无所谓

    what's in it for me? 这对我有什么好处?

  6. 正确理解 AsyncTask,Looper,Handler三者之间的关系(基于android 4.0)

    Looper 和Handler 是理解好AsyncTask的一个基础,我们可以先从这里开始,先给出一个主线程和子线程互相通信的例子. package com.example.loopertest; i ...

  7. nodejs简单层级结构配置文件

    在NodeJS中使用配置文件,有几种比较不错的方案:第一种:文件格式使用json是毋容置疑的好方案.格式标准,易于理解,文件内容读取到内存之后,使用JSON的标准分析函数即可得到配置项.第二种:将配置 ...

  8. view的onFinishInflate()何时调用的?

    onFinishInflate 当View中所有的子控件均被映射成xml后触发 比如你 自定义一个view叫myView ,路径是,com.test.view.MyView,此view是继承Linea ...

  9. std::sort引发的core

    #include <stdio.h> #include <vector> #include <algorithm> #include <new> str ...

  10. Loadrunner脚本之C语言文件处理函数

    一.打开文件 打开文件使用库函数中的fopen函数.fopen函数会为要打开的文件新建一个流,然后返回一个指向file型对象的指针,该file型对象中保存了控制这个流所需要的信息. fp=fopen( ...