Message Forwarding
【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的更多相关文章
- iOS开发·runtime原理与实践: 消息转发篇(Message Forwarding) (消息机制,方法未实现+API不兼容奔溃,模拟多继承)...
本文Demo传送门: MessageForwardingDemo 摘要:编程,只了解原理不行,必须实战才能知道应用场景.本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的消 ...
- 【引】runtime全解析,P1:Programming Guide
h2.Overview Objective-C language defers as many decisions as it can from compile time and link time ...
- Objective-C Runtime
原文地址:http://tech.glowing.com/cn/objective-c-runtime/ 原作者:顾鹏 如有侵权,请联系本人删除 Objective-C Objective-C 扩展了 ...
- iOS运行时Runtime浅析
运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...
- Effective Objective-C 2.0 — 第12条:理解消息转发机制
11 条讲解了对象的消息传递机制 12条讲解对象在收到无法解读的消息之后会发生什么,就会启动“消息转发”(message forwarding)机制, 若对象无法响应某个选择子,则进入消息转发流程. ...
- iOS开发——高级篇——Objective-C特性:Runtime
Objective-C是基于C语言加入了面向对象特性和消息转发机制的动态语言,这意味着它不仅需要一个编译器,还需要Runtime系统来动态创建类和对象,进行消息发送和转发.下面通过分析Apple开源的 ...
- 李洪强iOS经典面试题上
李洪强iOS经典面试题上 1. 风格纠错题 修改完的代码: 修改方法有很多种,现给出一种做示例: // .h文件 // http://weibo.com/luohanchenyilong/ / ...
- 详解Objective-C runtime
感谢翻译小组成员wingpan热心翻译.本篇文章是我们每周推荐优秀国外的技术类文章的其中一篇.如果您有不错的原创或译文,欢迎提交给我们,更欢迎其他朋友加入我们的翻译小组(联系qq:2408167315 ...
- 招聘一个靠谱的ios
1. 风格纠错题 修改方法有很多种,现给出一种做示例: 最终改为: 下面对具体修改的地方, 2. 什么情况使用 weak 关键字,相比 assign 有什么不同? 什么情况使用 weak 关键字? 1 ...
随机推荐
- find-all-anagrams-in-a-string
https://leetcode.com/problems/find-all-anagrams-in-a-string/ package com.company; import java.util.A ...
- Java C++ Python PHP JS等各种语言中的INT最值
Java: Integer.MAX_VALUE; Integer.MIN_VALUE; C++ INT_MAX INT_MIN <limit.h> 有些其他头文件也有引用 Python & ...
- C# Winform DataGridView分页功能的实现
// 1.定义几个所需的公有成员: ; //每页显示行数 ; //总记录数 ; //页数=总记录数/每页显示行数 ; //当前页号 ; //当前记录行 DataSet ds = new DataSet ...
- poj2942 Knights of the Round Table 双连通分支 tarjan
题解:http://blog.csdn.net/lyy289065406/article/details/6756821 讲的很详细我就不多说了. 题目连接:http://poj.org/proble ...
- BZOJ 1787 紧急集合
LCA.注意细节. #include<iostream> #include<cstdio> #include<cstring> #include<algori ...
- Android如何获取系统高度、标题栏和状态栏高度
在android应用中,有时需要计算个View的位置,导致需要计算状态栏高度,标题栏高度等信息.为以后方便,在此做个简单记录. 晒代码前先了解一下android屏幕区域的划分,如下图(该图引用自此文h ...
- 【解题报告】zju-1145 Dreisam Equations
原题地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=145 题目大意:在给定的等式右边数字之间加上加.减.乘运算符,使等式成 ...
- fzu 2128 AC自动机
链接 http://acm.fzu.edu.cn/problem.php?pid=2128 解题方法 首先考虑暴力,,就是拿每一个字符串在匹配串里面找到所有位置,然后从头到尾不断更新最长的合理位 ...
- centos6 下安装xfce+vnc
CentOS 安装图形界面的过程,简单记录一下.这里提供了两种图形界面的安装,分别是CentOS自带的gnome桌面及轻巧的xfce.据测试,我的精简版CentOS 6 64位系统安装gnome需要下 ...
- 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.6. 安装Oracle所依赖的必要包
2.6. 安装Oracle所依赖的必要包 2.6.1. 检查Oracle所依赖的必要rpm包 [root@localhost /]#rpm -q binutils compat-libstdc elf ...