消息异常处理
2016-05-20 08:42
218 查看
#import "ViewController.h" #import <objc/runtime.h> #import "AppDelegate.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // 调用一个没有实现的方法 [self performSelector:@selector(haha)]; } /*处理不能识别的方法,会经历如下步骤,前一个步骤走不通的时候就会走下一个步骤*/ #if 1 //step1:决议机制,是否为该sel动态的添加一个方法实现 + (BOOL)resolveInstanceMethod:(SEL)sel { NSString *selname = NSStringFromSelector(sel); if ([selname isEqualToString:@"haha"]) { class_addMethod(self, sel, class_getMethodImplementation([AppDelegate class], @selector(haha)), method_getTypeEncoding(class_getInstanceMethod([AppDelegate class], @selector(haha)))); return YES; } return [super resolveInstanceMethod:sel]; } - (void)oklala { NSLog(@"oklala"); } #endif #if 1 // step2:转发给其它的对象处理 - (id)forwardingTargetForSelector:(SEL)aSelector { return [[AppDelegate alloc] init]; } #endif #if 1 // step3: 封装成NSInvocation对象转发处理,系统默认是调用doesNotRecognizeSelector,重写forwardInvocation:方法同时必需重写methodSignatureForSelector:(构造NSInvocation对象必需先有一个NSMethodSignature对象), - (void)forwardInvocation:(NSInvocation *)anInvocation { NSLog(@"~~~~%@", [anInvocation description]); } - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector { NSLog(@"%@", NSStringFromSelector(aSelector)); NSMethodSignature *sig = [NSMethodSignature signatureWithObjCTypes:"@@:"]; return sig; } #endif @end
相关文章推荐
- Ant_的最完整build.xml解释
- $(function(){})和$(document).ready(function(){})
- [转][色彩 A] – 永远不要使用纯黑
- .NET Framework 4和.NET Framework 4 Client Profile的区别与联系
- acm 3 1012 骨牌方格
- HTML5安全:CORS(跨域资源共享)简介
- [Java A] – is not an enclosing class
- [Android Exception A] – 1-The following classes could not be instantiated
- Apache 与Tomcat 服务器联合搭建一个稳定,动态,高效的web服务器
- Quartz CronExpression表达式
- Java通过ASCII码判断是否为数字
- Android 图片切割工具类
- 架构师之路(7)---里氏代换原则
- Top 6 Programming Languages for Mobile App Development
- JS的深克隆
- Struts2框架action路径问题心得----》页面url请求怎么找action
- [译]Spring构建微服务
- 338CountingBits
- 如何写好一份竞品运营分析报告?
- 骆驼拼写法(CamelCase)