美文网首页
简单的runtime替换方法

简单的runtime替换方法

作者: 公子无礼 | 来源:发表于2017-05-02 16:27 被阅读0次

Method得到类的类方法

  1. 导入#import <objc/runtime.h>
+ (void)load {
    // 获取替换后的类方法
    Method otherMethod = class_getClassMethod([UIImage class], @selector(imageNameNextWith:));
    // 获取替换前的类方法
    Method method = class_getClassMethod([UIImage class], @selector(imageNamed:));
    // 然后交换类方法
    method_exchangeImplementations(otherMethod, method);
}

load方法只会走一次,利用runtime的method进行方法的替换   [uiimage class]可以写成self  当然这里分类可以直接写成
// 把系统的方法替换成我们自己写的方法
+ (UIImage *)imageNameNextWith:(NSString *)nameString {
    UIImage *image = nil;
    image = [UIImage imageNameNextWith:[nameString stringByAppendingString:@"tupian.jpg"]];
    return image;
}
// 这里的imagenamed其实已经替换成了我们自己写的方法
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
image.image = [UIImage imageNamed:@"1.jpg"];
[self.view addSubview:image];

------收集

相关文章

网友评论

      本文标题:简单的runtime替换方法

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