iOS 利用runTime进行“私有方法”替换

写在前面

首先声明:题目中所说的“私有方法”只是我们感官上的感觉,OC 中没有绝对的私有变量和私有方法。

关于私有变量和“私有方法”

  • 私有变量 用@private来声明私有变量,只允许本类访问。
  • “私有方法”OC中没有提供关键字来声明私有方法,可以通过category的匿名类Extension通过在一个只在类的.m文件中来声明一个只能被本类访问的方法。

但是要注意的是:OC是动态性语言,他的对象类型和真正要调用的方法是在运行时才确定的,所以这就决定了在oc中没有绝对的私有变量和私有方法的,通过runtime我们可以动态的去对类中所有的变量和方法进行操作,关于runtime的一些东西在
《iOS-RunTime介绍及使用 http://www.jianshu.com/p/5408dcab8f02》中介绍。比如通过class_copyIvarListclass_copyMethodList这两个方法。

正题

我们在项目中可能会遇到这样的情况,你引入了一个库,库里面的.m文件中的方法你不能修改,但是你又必须要用到,就像我们遇到的场景,我需要跳到库里的mainViewController中,在mainViewController有个navigationController的pop方法,在这个mainViewController中定义的pop方法是popToRootViewControllerAnimated但是我需要的是pop到指定viewController中,但是我们又不能修改mainViewController这个文件中的方法,该怎么办呢?这里说一个利用runtime解决的方法:

替换“私有方法”

在我们当前的VC中,我们生命一个方法changeLiveMethod用来执行替换方法操作。
代码如下:

  • 新的pop方法
- (void)popToLiveListVC1
{
    UIViewController *target = nil;
    for (UIViewController * controller in self.navigationController.viewControllers) { //遍历
        if ([controller isKindOfClass:[LiveListViewController class]]) { //这里判断是否为你想要跳转的页面
            target = controller;
        }
    }
    if (target)
    {
        [self.navigationController popToViewController:target animated:YES]; //跳转
    }
}
  • 替换方法
//当前pop方法1
    SEL thisSelector = @selector(popToLiveListVC1);
    Method thisMethod = class_getInstanceMethod([self class], thisSelector);
    
   //获取mainViewCOntroller 中的gotoViewController方法
//    unsigned int count = 0;
//    Method *membFuns = class_copyMethodList([MainViewController class], &count);
    NSString *mainPopMethodStr = @"gotoViewController";
//    for (int i =0 ; i < count; i++) {
//        SEL name = method_getName(membFuns[i]);
//        NSString *methodName = [NSString stringWithCString:sel_getName(name) encoding:NSUTF8StringEncoding];
//        NSLog(@"m method:%@",methodName);
//        if ([methodName isEqualToString:@"gotoViewController"]) {
//            mainPopMethodStr = methodName;
//        }
//    }
    SEL mainPopSelector = NSSelectorFromString(mainPopMethodStr);
    Method mainPopMethod = class_getInstanceMethod([MainViewController class], mainPopSelector);
    method_exchangeImplementations(thisMethod, mainPopMethod);

代码分析: 先取到当前我们需要的新方法 Method,然后获取库里面MainViewController私有方法,我们用Method mainPopMethod = class_getInstanceMethod([MainViewController class], @selector(gotoViewController));这个方法编译器会报个警告,所以我们用SEL的这个方式获取这个mainPopMethod ,然后进行method_exchangeImplementations方法的交换,这个方法就是改变类的分发表,使它在解析消息的时候,将一个选择器selector对应到另一个替换的实现,并且将该选择器对应的原始实现关联到新的选择器中。

对SEL Method IMP的一些说明

  • 选择器(Selector-typedef struct objc_selector *SEL ):用于在运行时表示一个方法的名称,一个方法选择器就是一个C字符串,在运行时会被注册或者映射,选择器是由编译器生成的,并在类被加载的时候由运行时自动进行映射。
  • 方法(Method-typedef struct objc_method *Method):在类的定义中代表一个方法的类型。
  • 实现(Implementation- typedef id (*IMP)(id, SEL, ...)):这是一个指向方法实现函数起始地址的指针,这个函数的第一个参数是指向self的指针,第二个参数是方法选择器,然后是方法的参数。
    理解它们之间关系的最好方式是:一个类维护着一张分发表(dispatch table),用来解析运行时发来的消息;该表的每个入口是一个方法(Method),其中的key就是选择器(SEL),对应一个实现(IMP),即一个指向底层c函数的指针。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值