美文网首页
runtime个人见解

runtime个人见解

作者: 高乔人 | 来源:发表于2018-04-28 23:16 被阅读13次

Runtime简称运行时,OC就是运行时机制,也就是运行时候的一些机制,其中最重要的就是 消息机制。

对于C语言,函数的调用在编译的时候会决定哪个函数调用

对于OC的函数,动态函数,在编译的时候并不能真正的决定调用哪个函数,只有在真正运行的时候,才会根据函数名找到对用的函数来调用。

runtime作用 

1.方法调用的本质,就是让对象发送消息。objc_msgSend,只有对象才能发送消息,故objec开头

2.使用消息机制前提,必须导入import<objc/message.h>

  Person*p = [[Personalloc]init];

    objc_msgSend(p,@selector(eat));

    objc_msgSend(p,@selector(run:),30);

方法调用的流程:对象方法:类对象的方法列表里面     类方法:元类方法列表中

比如eat方法:1.通过isa指针去对用的类中查找  2.把方法名转化成方法编号  3.根据方法标号去查找对应的方法

runtime动态交换方法:(image的分类中这样写)

+ (void)load{

  MethodimgeMethod =class_getClassMethod(self,@selector(imageNamed:));

  MethodxhwImageMethod =  class_getClassMethod(self,@selector(xhw_imageNamed:));

  method_exchangeImplementations(imgeMethod, xhwImageMethod);

}

+ (UIImage*)xhw_imageNamed:(NSString*)name{

//xhw_imageNamed:会去掉用imageName这个方法,

    UIImage*image = [UIImage xhw_imageNamed:name];

    if(image==nil) {

        NSLog(@"加载失败");

    }else{

        NSLog(@"加载成功");

    }

    returnimage;

}

相关文章

网友评论

      本文标题:runtime个人见解

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