iOS之《Effective Objective-C 2.0》读书笔记(12)
来源:互联网 发布:电脑c盘数据恢复 编辑:程序博客网 时间:2024/06/05 05:05
第12条:理解消息转发机制
1.消息转发机制的作用
对象在接受到无法解读的消息时,会在编译时报警告,运行时直接崩溃,此方法可以给对象和消息更多的机会来完成成功的调用
2.消息转发流程
(1)动态方法解析:
+(BOOL)resolveInstanceMethod:(SEL)sel // 实例方法+(BOOL)resolveClassMethod:(SEL)sel // 类方法
- 下列代码演示了如何用“resolveInstanceMethod:”来实现@dynamic属性:
id autoDictionaryGetter(id self, SEL _cmd); void autoDictionarySetter(id self, SEL _cmd, id value); + (BOOL)resolveInstanceMethod:(SEL)selector { NSString *selectorString = NSStringFromSelector(selector); if ( /* selector is from a @dynamic property */ ) { if ([selectorString hasPrefix:@"set"]) { class_addMethod(self, selector, (IMP)autoDictionarySetter, "v@:@"); } else { class_addMethod(self, selector, (IMP)autoDictionaryGetter, "@@:"); } return YES; } return [super resolveInstanceMethod:selector]; }
(2)备援接受者:运行期系统把消息转给其他接收者处理
- (id)forwardingTargetForSelector:(SEL)aSelector{ class *other = [class new]; if ([other forwardingTargetForSelector:aSelector]) { return other; }else{ return [super forwardingTargetForSelector:aSelector]; }}
(3)完整的消息转发:创建NSInvocation对象,把与尚未处理的那条消息有关的全部细节都封于其中。此对象包含选择子、目标target及参数。在触发NSInvocation对象时,消息派发系统会把消息指派给目标对象
- TestModel
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { if(aSelector == @selector(testMethod)) { return [NSMethodSignature signatureWithObjCTypes:"v@:"]; } return nil; } -(void)forwardInvocation:(NSInvocation *)anInvocation { if (anInvocation.selector == @selector(testMethod)) { TestModelHelper1 *h1 = [[TestModelHelper1 alloc] init]; TestModelHelper2 *h2 = [[TestModelHelper2 alloc] init]; [anInvocation invokeWithTarget:h1]; [anInvocation invokeWithTarget:h2]; } }
- TestModelHelper1
-(void)testMethod { NSLog(@"i am TestModelHelper1"); }
- TestModelHelper2
-(void)testMethod { NSLog(@"i am TestModelHelper2"); }
- 主调用类 : TestModel本身没有实现testMethod方法,但最终运行后,程序没有报错,且TestModelHelper1和TestModelHelper2的testMethod方法都被执行了
TestModel *model = [[TestModel alloc] init]; [model testMethod];
(4)消息未能处理:调用doesNotRecognizeSelector抛出异常
阅读全文
0 0
- iOS之《Effective Objective-C 2.0》读书笔记(12)
- <iOS读书笔记>之Effective Objective-C 2.0
- iOS之《Effective Objective-C 2.0》读书笔记(1)
- iOS之《Effective Objective-C 2.0》读书笔记(2)
- iOS之《Effective Objective-C 2.0》读书笔记(3)
- iOS之《Effective Objective-C 2.0》读书笔记(4)
- iOS之《Effective Objective-C 2.0》读书笔记(5)
- iOS之《Effective Objective-C 2.0》读书笔记(6)
- iOS之《Effective Objective-C 2.0》读书笔记(7)
- iOS之《Effective Objective-C 2.0》读书笔记(8)
- iOS之《Effective Objective-C 2.0》读书笔记(9)
- iOS之《Effective Objective-C 2.0》读书笔记(10)
- iOS之《Effective Objective-C 2.0》读书笔记(11)
- iOS之《Effective Objective-C 2.0》读书笔记(13)
- iOS之《Effective Objective-C 2.0》读书笔记(14)
- iOS之《Effective Objective-C 2.0》读书笔记(16)
- iOS之《Effective Objective-C 2.0》读书笔记(17)
- iOS之《Effective Objective-C 2.0》读书笔记(21)
- 图像区域特征
- 重度游戏热度居高不下,中轻度游戏如何突出重围?
- Yii
- DOM解析XML
- 2017年6月英语 CET 四六级考试查询准考证方法
- iOS之《Effective Objective-C 2.0》读书笔记(12)
- 为什么电脑会突然蓝屏
- 扫二维码跳转到指定页面(将url转化成二维码)
- C++虚函数表
- JAVA环境配置及再次配置后cmd内版本不一致问题
- QGC之qml学习
- hdu 6030 Happy Necklace
- 写程序的小技巧
- Linearizability 一致性验证