美文网首页
runtime 动态添加方法

runtime 动态添加方法

作者: liuyihua的技术修炼场 | 来源:发表于2018-03-20 23:39 被阅读15次

    runtime 动态添加方法

    定义一个Person类,但是在类中并有相关方法的声明和实现;直接在ViewController中进行对象创建和使用:

    #import "ViewController.h"
    .
    .
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        Person * p = [[Person alloc] init];
        
        //给p对象发送一个没有实现的实例方法,并带有参数
        [p performSelector:@selector(eat:) withObject:@"汉堡!!"];
        
        // 我们正常的OC调用函数方法,其实底层就是发送信息的一个过程:objc_msgSend
        // [p eat:@"汉堡"];
        // objc_msgSend(p,@select(eat:),@"汉堡");
        
    }
    
    

    此时在Person类中并相关方法的实现,但是Person类是继承自NSObjct的根类,有两个类方法:

    //作用:如果该类接收到一个没有实现的实例方法,就会来到这里
    + (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    
    //作用:如果该类接收到一个没有实现的类方法,就会来到这里
    + (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
    

    上面的控制器中我们发送的是一个实例对象,那个这里在打印一下方法:

    //如果该类接收到一个没有实现的实例方法,就会来到这里
    +(BOOL)resolveInstanceMethod:(SEL)sel
    {
         NSLog(@"%@",NSStringFromSelector(sel)); 
        /*
        动态添加一个方法!!
         1.cls 哪个类
         2.SEL
         3.IMP
         4.返回值类型!
         */
          class_addMethod(self, sel, (IMP)haha, "v@:@");
         
         // class_addMethod(<#Class  _Nullable __unsafe_unretained cls#>, <#SEL  _Nonnull name#>, <#IMP  _Nonnull imp#>, <#const char * _Nullable types#>)
    
         return [super resolveInstanceMethod:sel];
    }
    
    //实现的方法
    void haha(id obj,SEL sel,NSString * objc){
        NSLog(@"%@--%@--%@",[obj class],NSStringFromSelector(sel),objc);
    }
    
    

    打印结果:


    打印结果.png

    疑问: class_addMethod(self, sel, (IMP)haha, "v@:@");参数中的 "v@:@" 是什么意思?各个参数有具体是什么意思,我这查看了文档,文档原文如下:

    Parameters
    cls
    The class to which to add a method.//要添加方法的类
    
    name
    A selector that specifies the name of the method being added.//指定要添加的方法名称的选择器
    
    imp
    A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.//它是一个函数,它是新方法的实现。这个函数至少需要两个参数——self和_cmd。
    
    types
    An array of characters that describe the types of the arguments to the method. For possible values, see Objective-C Runtime Programming Guide > Type Encodings. Since the function must take at least two arguments—self and _cmd, the second and third characters must be “@:” (the first character is the return type).
    //一组字符,用来描述方法参数的类型。对于可能的值,请参见Objective-C运行时编程指南>类型编码。由于该函数必须至少使用两个arguments-self和_cmd,所以第二个和第三个字符必须是“@:”(第一个字符是返回类型)。
    
    

    类型编码部分:


    类型编码部分.png

    那IMP又是什么?

    IMP是”implementation”的缩写,它是objetive-C 方法(method)实现代码块的地址,可像C函数一样直接调用。通常情况下我们是通过[object method:parameter]或objc_msgSend()的方式向对象发送消息,然后Objective-C运行时(Objective-C runtime)寻找匹配此消息的IMP,然后调用它;但有些时候我们希望获取到IMP进行直接调用。
    具体可参考这:关于Objective-C方法的IMP

    相关文章

      网友评论

          本文标题:runtime 动态添加方法

          本文链接:https://www.haomeiwen.com/subject/haiiqftx.html