iOS NSObject中forwardInvocation消息重定向
来源:互联网 发布:贝叶斯分类算法原理 编辑:程序博客网 时间:2024/06/06 08:43
NSObject是大多数系统 api的基类,现在介绍一下它的forwardInvocation消息重定向功能。
在obj-c中我们可以向一个实例发送消息,实例收到消息后会进行一些处理。比如我们想调用一个方法,便向这个实例发送一个消息,实例收到消息后,如果能respondsToSelector,那么就会调用相应的方法。如果不能respond一般情况下会crash。今天要的,就是不让它crash。
首先说一下向一个实例发送一个消息后,系统是处理的流程:
1. 发送消息如:[self startwork]
2. 系统会check是否能response这个消息
3. 如果能response则调用相应方法,不能则抛出异常
在第2步中,系统是如何check实例是否能response消息呢?如果实例本身就有相应的response,那么就会相应之,如果没有系统就会发出methodSignatureForSelector消息,寻问它这个消息是否有效?有效就返回对应的方法地址之类的,无效则返回nil。如果是nil就会crash, 如果不是nil接着发送forwardInvocation消息。
所以我们在重写methodSignatureForSelector的时候就人工让其返回有效实例。
我们知道消息重定向的执行流程了,我们再来看看什么时候会用到消息重定向。
- 模拟多继承
重定向消息可以模拟多继承。一个对象已经继承一个父类了,还行执行其他类的方法,怎么办,我们可以考虑用重定向。
- 代理:NSProxy类
我们定义这样一个类
@interface TargetProxy : NSProxy { id realObject1; id realObject2; } - (id)initWithTarget1:(id)t1 target2:(id)t2; @end
实现:
@implementation TargetProxy - (id)initWithTarget1:(id)t1 target2:(id)t2 { realObject1 = [t1 retain]; realObject2 = [t2 retain]; return self; } - (void)dealloc { [realObject1 release]; [realObject2 release]; [super dealloc]; } // The compiler knows the types at the call site but unfortunately doesn't // leave them around for us to use, so we must poke around and find the types // so that the invocation can be initialized from the stack frame. // Here, we ask the two real objects, realObject1 first, for their method // signatures, since we'll be forwarding the message to one or the other // of them in -forwardInvocation:. If realObject1 returns a non-nil // method signature, we use that, so in effect it has priority. - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSMethodSignature *sig; sig = [realObject1 methodSignatureForSelector:aSelector]; if (sig) return sig; sig = [realObject2 methodSignatureForSelector:aSelector]; return sig; } // Invoke the invocation on whichever real object had a signature for it. - (void)forwardInvocation:(NSInvocation *)invocation { id target = [realObject1 methodSignatureForSelector:[invocation selector]] ? realObject1 : realObject2; [invocation invokeWithTarget:target]; } // Override some of NSProxy's implementations to forward them... - (BOOL)respondsToSelector:(SEL)aSelector { if ([realObject1 respondsToSelector:aSelector]) return YES; if ([realObject2 respondsToSelector:aSelector]) return YES; return NO; } @end
现在我们还用这个类,注意向它发送的消息:
// Create a proxy to wrap the real objects. This is rather // artificial for the purposes of this example -- you'd rarely // have a single proxy covering two objects. But it is possible. id proxy = [[TargetProxy alloc] initWithTarget1:string target2:array]; // Note that we can't use appendFormat:, because vararg methods // cannot be forwarded! [proxy appendString:@"This "]; [proxy appendString:@"is "]; [proxy addObject:string]; [proxy appendString:@"a "]; [proxy appendString:@"test!"]; NSLog(@"count should be 1, it is: %d", [proxy count]); if ([[proxy objectAtIndex:0] isEqualToString:@"This is a test!"]) { NSLog(@"Appending successful."); } else { NSLog(@"Appending failed, got: '%@'", proxy); }
运行的结果是:count should be 1, it is: 1
Appending successful.
TargetProxy声明中是没有appendString与addObject消息的,在这儿却可以正常发送,不crash,原因就是发送消息的时候,如果原本类没有这个消息响应的时候,转向询问methodSignatureForSelector,接着在forwardInvocation将消息重定向。
一个proxy对象既可以当NSString用,又可以当NSMultableArray用!那么我们什么时候用代理的重定向功能呢?官方说明里举了两个例子,实现透明分布式消息(如NSDistantObject)和懒加载非常耗时的对象。
forwardInvocation:方法就像一个不能识别的消息的分发中心,将这些消息转发给不同接收对象。或者它也可以象一个运输站将所有的消息都发送给同一个接收对象。它可以将一个消息翻译成另外一个消息,或者简单的"吃掉“某些消息,因此没有响应也没有错误。forwardInvocation:方法也可以对不同的消息提供同样的响应,这一切都取决于方法的具体实现。该方法所提供是将不同的对象链接到消息链的能力。
参考:https://developer.apple.com/library/content/samplecode/ForwardInvocation/Listings/main_m.html#//apple_ref/doc/uid/DTS40008833-main_m-DontLinkElementID_4
- iOS NSObject中forwardInvocation消息重定向
- NSObject中forwardInvocation消息重定向
- Objective-C 消息转发 与NSObject中forwardInvocation消息重定向
- NSObject中methodSignatureForSelector、forwardInvocation两个消息方法
- NSProxy中methodSignatureForSelector、forwardInvocation两个消息方法
- 消息转发 forwardInvocation
- 消息转发 forwardInvocation
- iOS中NSlog重定向到文件
- ios ASIHttpRequest 重定向
- iOS url重定向
- iOS--日志重定向
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- iOS中id与NSObject* 和id<NSObject>的区别
- forwardInvocation
- iOS runtime forwardInvocation一些总结
- cs231n assignment1--Softmax
- getCanonicalFile getAbsolutePath getPath
- ASTableNode+MJRefresh教程
- ch5 link analysis
- 深入嵌入式系统的bootloader
- iOS NSObject中forwardInvocation消息重定向
- Node.js简介
- [Spring实战系列]
- Unit 4-Lecture 5: Expectation
- Android属性动画完全解析(上),初识属性动画的基本用法
- noi-8175-将字符串中的小写字母转换成大写字母
- 下载网页视频
- jQuery的加法运算
- MyBatis参数传入集合之foreach动态sql