美文网首页
Runtime的方法交换

Runtime的方法交换

作者: 成长的船 | 来源:发表于2017-08-07 23:51 被阅读0次

    Runtime知道如何用runtime交换方法,但是思考了下,没有找到非常好的实际价值,可能实力不够吧。先写下来,作为积累。

    
    +(void)load
    {
    //class_getClassMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>) //获取方法名
    //method_exchangeImplementations(<#Method m1#>, <#Method m2#>)          //交换方法
    
    Method system_imageNamed = class_getClassMethod([UIImage class], @selector(imageNamed:));
    
    Method yh_imageNamed = class_getClassMethod([UIImage class], @selector(yh_imageNamed:));
    
    method_exchangeImplementations(system_ImageNamed, yh_ImageNamed);
    
    }
    
    + (UIImage *)yh_imageNamed:(NSString *)name
    {
        UIImage * image = [UIImage yh_imageNamed:name];
        if (image == nil) {
            NSLog(@"加载的图片名为空");
         }
        return image;
    }
    
    @end
    
    

    再一个,在+(void)load这个方法里写方法的交换,原因是load方法只会执行一次,刚好符合交换方法的要求。


    相关文章

      网友评论

          本文标题:Runtime的方法交换

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