Runtime(二)动态添加方法以及消息转发机制
2016-03-11 16:47
573 查看
runtime中函数的调用:
下面一张图详细的概括了每个函数调用的先后以及执行的前提:
1.对象在收到无法解读的消息后,首先会调用所属类的 + (BOOL)resolveInstanceMethod:(SEL)sel 这个方法在运行时,没有找到SEL的IML时就会执行。这个函数是给类利用class_addMethod添加函数的机会。根据文档,如果实现了添加函数代码则返回YES,未实现返回NO。
新建工程代码如下:
运行,程序崩溃,因为没有找到resolveAdd:这个方法。下面我们在里面实现 + (BOOL)resolveInstanceMethod:(SEL)sel这个方法,并且判断如果SEL 是doSomething那就输出add C IMP
2.如果在+ (BOOL)resolveInstanceMethod:(SEL)sel中没有找到或者添加方法
消息继续往下传递到- (id)forwardingTargetForSelector:(SEL)aSelector看看是不是有对象可以执行这个方法,我们来重新建个工程,然后新建一个叫secondViewController的类,里面有一个- (void)secondVCMethod方法
secondViewController中.h和.m的代码:
ViewController中:
当然,还有好几个函数,在上面那张图里面已经清晰的表达了,就不一一解读了。
下面一张图详细的概括了每个函数调用的先后以及执行的前提:
1.对象在收到无法解读的消息后,首先会调用所属类的 + (BOOL)resolveInstanceMethod:(SEL)sel 这个方法在运行时,没有找到SEL的IML时就会执行。这个函数是给类利用class_addMethod添加函数的机会。根据文档,如果实现了添加函数代码则返回YES,未实现返回NO。
新建工程代码如下:
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(resolveAdd:) withObject:@"test"]; }
运行,程序崩溃,因为没有找到resolveAdd:这个方法。下面我们在里面实现 + (BOOL)resolveInstanceMethod:(SEL)sel这个方法,并且判断如果SEL 是doSomething那就输出add C IMP
@interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [self performSelector:@selector(resolveAdd:) withObject:@"test"]; }
+ (BOOL)resolveInstanceMethod:(SEL)sel {
//给本类动态添加一个方法
// if ([NSStringFromSelector(sel) isEqualToString:@"resolveAdd:"])
if (sel == @selector(resolveAdd:))
{
class_addMethod([self class], sel, (IMP)runAddMethod, "v@:*");
}
return YES;
}
void runAddMethod(id self, SEL _cmd, NSString *string){
NSLog(@"add C IMP ", string);
}
/*程序不在崩溃 打印结果: 2016-03-08 11:23:39.057 RunTimeDemo1[1547:432945] add C IMP 这时候就说明我们已经通过runtime成功的向我们这个类中添加了一个方法 关于class_addMethod这个方法,是这样定义的: OBJC_EXPORT BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types) Class cls 给哪个类添加方法,本例中是self SEL name 添加的方法,本例中是重写的拦截调用传进来的selector IMP imp 实现这个方法的函数,C方法的方法实现可以直接获得。如果是OC方法,可以用+ (IMP)instanceMethodForSelector:(SEL)aSelector;获得方法的实现 types 定义该数返回值类型和参数类型的字符串,这里比如"v@:",其中v就是void,带表返回类型就是空,@代表参数,这里指的是id(self),这里:指的是方法SEL(_cmd) */
2.如果在+ (BOOL)resolveInstanceMethod:(SEL)sel中没有找到或者添加方法
消息继续往下传递到- (id)forwardingTargetForSelector:(SEL)aSelector看看是不是有对象可以执行这个方法,我们来重新建个工程,然后新建一个叫secondViewController的类,里面有一个- (void)secondVCMethod方法
secondViewController中.h和.m的代码:
#import <UIKit/UIKit.h> @interface SecondViewController : UIViewController @end
#import "SecondViewController.h" @interface SecondViewController () @end @implementation SecondViewController - (void)viewDidLoad { [super viewDidLoad]; } - (void)secondVCMethod { NSLog(@"这是SecondViewController中的方法"); }
ViewController中:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /* 2.如果在+ (BOOL)resolveInstanceMethod:(SEL)sel中没有找到或者添加方法 消息继续往下传递到- (id)forwardingTargetForSelector:(SEL)aSelector看看是不是有对象可以执行这个方法,我们来重新建个工程,然后新建一个叫SecondViewController的类,里面有一个- (void)secondVCMethod方法, */ /* 现在我想在ViewController中调用- (void)secondVCMethod这个方法,我们知道ViewController和SecondViewController并无继承关系,按照正常的步骤去做程序肯定会因为在ViewController找不到- (void)secondVCMethod这个方法而直接崩溃的 */ [self performSelector:@selector(secondVCMethod)]; }
+ (BOOL)resolveInstanceMethod:(SEL)sel { return [super resolveInstanceMethod:sel]; }
- (id)forwardingTargetForSelector:(SEL)aSelector { Class class = NSClassFromString(@"SecondViewController"); UIViewController *vc = class.new; if (aSelector == NSSelectorFromString(@"secondVCMethod")) { NSLog(@"secondVC do this !"); return vc; } return nil; } /*在没有找到- (void)secondVCMethod这个方法的时候,消息继续传递,直到- (id)forwardingTargetForSelector:(SEL)aSelector,然后我在里面创建了一个SecondViewController的对象,并且判断如过有这个方法,就返回SecondViewController的对象。这个函数就是消息的转发,在这儿我们成功的把消息传给了SecondViewController,然后让它来执行,所以就执行了那个方法。同时,也相当于完成了一个多继承! */
当然,还有好几个函数,在上面那张图里面已经清晰的表达了,就不一一解读了。
相关文章推荐
- memcache 更新
- 浅谈对软件工程的认识与理解
- PD安装不同Linux并安装Apache2+PHP5+MySQL和测试SeLinux
- Java_Iterable_Iterator_foreach学习笔记
- Android中 startService()和bindService()的区别
- 运维朋友们,别再问需不需要学Python!
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- Git学习笔记
- 2016峰会:项目管理与高级项目管理 - 图片花絮
- 推荐系统中的相似性度量
- [Machine Learning] Random Forest 随机森林
- cocoa开发之:利用CABasicAnimation实现图片旋转
- 事务,视图及索引!!!
- spark.driver.maxResultSize || java.lang.OutOfMemoryError
- 2016C#判断是否为闰年
- 输入字符串,按由小到大的顺序输出
- SVN--(Eclipse)在历史记录中比较版本差异
- 关于dsp/bios的hwi和ecm的问题
- c++基础--容器适配器
- java的if-else和while用法