在obj-c中我们可以向一个实例发送消息,相当于c/c++ java中的方法调用,只不过在这儿是说发送消息,实例收到消息后会进行一些处理。比如我们想调用一个方法,便向这个实例发送一个消息,实例收到消息后,如果能respondsToSelector,那么就会调用相应的方法。如果不能respond一般情况下会crash。今天要的,就是不让它crash。

首先说一下向一个实例发送一个消息后,系统是处理的流程:

1. 发送消息如:[self startwork]

2. 系统会check是否能response这个消息

3. 如果能response则调用相应方法,不能则抛出异常

在第二步中,系统是如何check实例是否能response消息呢?如果实例本身就有相应的response,那么就会相应之,如果没有系统就会发出methodSignatureForSelector消息,寻问它这个消息是否有效?有效就返回对应的方法地址之类的,无效则返回nil。如果是nil就会crash, 如果不是nil接着发送forwardInvocation消息。

所以我们在重写methodSignatureForSelector的时候就人工让其返回有效实例。  文字说不清,还是用代码说明

我们定义了这样一个类

  1. @interface TargetProxy : NSProxy {
  2. id realObject1;
  3. id realObject2;
  4. }
  5. - (id)initWithTarget1:(id)t1 target2:(id)t2;
  6. @end

实现:

  1. @implementation TargetProxy
  2. - (id)initWithTarget1:(id)t1 target2:(id)t2 {
  3. realObject1 = [t1 retain];
  4. realObject2 = [t2 retain];
  5. return self;
  6. }
  7. - (void)dealloc {
  8. [realObject1 release];
  9. [realObject2 release];
  10. [super dealloc];
  11. }
  12. // The compiler knows the types at the call site but unfortunately doesn't
  13. // leave them around for us to use, so we must poke around and find the types
  14. // so that the invocation can be initialized from the stack frame.
  15. // Here, we ask the two real objects, realObject1 first, for their method
  16. // signatures, since we'll be forwarding the message to one or the other
  17. // of them in -forwardInvocation:.  If realObject1 returns a non-nil
  18. // method signature, we use that, so in effect it has priority.
  19. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
  20. NSMethodSignature *sig;
  21. sig = [realObject1 methodSignatureForSelector:aSelector];
  22. if (sig) return sig;
  23. sig = [realObject2 methodSignatureForSelector:aSelector];
  24. return sig;
  25. }
  26. // Invoke the invocation on whichever real object had a signature for it.
  27. - (void)forwardInvocation:(NSInvocation *)invocation {
  28. id target = [realObject1 methodSignatureForSelector:[invocation selector]] ? realObject1 : realObject2;
  29. [invocation invokeWithTarget:target];
  30. }
  31. // Override some of NSProxy's implementations to forward them...
  32. - (BOOL)respondsToSelector:(SEL)aSelector {
  33. if ([realObject1 respondsToSelector:aSelector]) return YES;
  34. if ([realObject2 respondsToSelector:aSelector]) return YES;
  35. return NO;
  36. }
  37. @end

现在我们还用这个类,注意向它发送的消息:

  1. // Create a proxy to wrap the real objects.  This is rather
  2. // artificial for the purposes of this example -- you'd rarely
  3. // have a single proxy covering two objects.  But it is possible.
  4. id proxy = [[TargetProxy alloc] initWithTarget1:string target2:array];
  5. // Note that we can't use appendFormat:, because vararg methods
  6. // cannot be forwarded!
  7. [proxy appendString:@"This "];
  8. [proxy appendString:@"is "];
  9. [proxy addObject:string];
  10. [proxy appendString:@"a "];
  11. [proxy appendString:@"test!"];
  12. NSLog(@"count should be 1, it is: %d", [proxy count]);
  13. if ([[proxy objectAtIndex:0] isEqualToString:@"This is a test!"]) {
  14. NSLog(@"Appending successful.");
  15. } else {
  16. NSLog(@"Appending failed, got: '%@'", proxy);
  17. }

运行的结果是:

count should be 1, it is:  1

Appending successful.

TargetProxy声明中是没有appendString与addObject消息的,在这儿却可以正常发送,不crash,原因就是发送消息的时候,如果原本类没有这个消息响应的时候,转向询问methodSignatureForSelector,接着在forwardInvocation将消息重定向。 上面也说了多参数的消息是不能重定向的。不过貌似要实现这个要关掉ARC。

利用forwardInvocation实现消息重定向的更多相关文章

  1. 利用System V消息队列实现回射客户/服务器

    一.介绍 在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如: 1. 服务器必须启动之时,客户端才能连上服务端,并 ...

  2. Android中利用Handler实现消息的分发机制(三)

    在第二篇文章<Android中利用Handler实现消息的分发机制(一)>中,我们讲到主线程的Looper是Android系统在启动App的时候,已经帮我们创建好了,而假设在子线程中须要去 ...

  3. 利用rabbit_mq队列消息实现对一组主机进行命令下发

    目的: 利用rabbit_mq队列消息实现对一组主机进行命令下发 server: #!/usr/bin/env python3.5 # -*- coding:utf8 -*- import os,sy ...

  4. 利用OC对象的消息重定向forwardingTargetForSelector方法构建高扩展性的滤镜功能

    在OC中,当像一个对象发送消息,而对象找到消息后,从它的类方法列表,父类方法列表,一直找到根类方法列表都没有找到与这个选择子对应的函数指针.那么这个对象就会触发消息转发机制. OC对象的继承链和isa ...

  5. 使用methodSignatureForSelector与forwardInvocation实现消息转发 (转)

    转自:http://blog.sina.com.cn/s/blog_8c87ba3b0102v006.html 在给程序添加消息转发功能以前,必须覆盖两个方法,即methodSignatureForS ...

  6. Objective-C 利用OC的消息机制,使用Method Swizzling进行方法修改

    功能:修改父类不可修改函数方法,函数方法交换 应用场景:假如我们使用的他人提供一个的framework,.m已被打包成二进制.a无法修改源码,只留下.h头文件,那假如代码中某个函数出现了问题可以通过这 ...

  7. jsp中利用response.senddirect(str)重定向,传递参数新思路

    用Servlet进行请求重定向,参数传递好办,直接用request.setAttribute(str1,str2); 但是如果不用Servlet 而是直接用jsp进行转发呢? 我们首先要知道   请求 ...

  8. Java利用Redis实现消息队列

    应用场景 为什么要用redis?二进制存储.java序列化传输.IO连接数高.连接频繁 一.序列化 这里编写了一个java序列化的工具,主要是将对象转化为byte数组,和根据byte数组反序列化成ja ...

  9. Springboot21 整合redis、利用redis实现消息队列

    1 前提准备 1.1 创建一个springboot项目 技巧01:本博文基于springboot2.0创建 1.2 安装redis 1.2.1 linux版本 参考博文 1.2.2 windows版本 ...

随机推荐

  1. JS和CSS加载(渲染)机制不同

    一.结论 CSS可以在页面加载完成后随时渲染.举个例子:通过js给某个元素加一个id或者css,只要这个id或者css有对应的样式,此元素的样式就会自动生效. JS不可以在页面加载完成后生效.最明显的 ...

  2. 基于Nginx的Rtmp流媒体服务器环境搭建

    一.编译安装 wget http://nginx.org/download/nginx-1.4.2.tar.gz wget https://github.com/arut/nginx-rtmp-mod ...

  3. CodeForces 590B Chip 'n Dale Rescue Rangers

    这题可以o(1)推出公式,也可以二分答案+验证. #include<iostream> #include<cstring> #include<cmath> #inc ...

  4. angular中控制器之间的通讯方式

    1, 利用作用域的继承方式 由于作用域的继承是基于js的原型继承方式,所以这里分为两种情况,当作用域上面的值为基本类型的时候,修改父作用域上面的值会 影响到子作用域,反之,修改子作用域只会影响子作用域 ...

  5. mousewheel,DOMMouseScroll判断滚轮滚动方向

    firefox使用DOMMouseScroll,其他浏览器使用mousewheel 首先绑定一个滚动事件 //firefox使用DOMMouseScroll,其他浏览器使用mousewheel$(do ...

  6. Git 解决同步 No value for key branch.master.merge found in

    [core] repositoryformatversion = 0 filemode = false logallrefupdates = true [remote "origin&quo ...

  7. java web系统优化, mysql查询优化

    1. 采用数据库连接池技术 2. 优化sql语句格式, 比如用PreparedStatement代替Statement, 前者避免重复编译, 后者每次都需要对数据库进行解析编译, 降低数据库的访问效率 ...

  8. 关于cin的用法一些小结

    在写二叉树的时候遇到if(!cin)那几个标志位弄得并不清楚,还遇到了诸如cin.clear()等函数,感觉C++又白学了,于是打算去网上搜了几篇靠谱的文章,有时候看来,一些事件处理类的工程代码,在A ...

  9. Java 基础类型转换byte数组, byte数组转换基础类型

    Java 基础类型转换byte数组, byte数组转换基础类型 Java类型转换 java类对象转化为byte数组

  10. Nodejs之模板ejs

    ejs使用说明及介绍. 1.创建ejs项目 express -e test  //创建模板为ejs的项目,默认为jade. 2.使用 app.js中添加 var ejs = require('ejs' ...