美文网首页
对NSArray或者NSMutableArray的方法交换的问题

对NSArray或者NSMutableArray的方法交换的问题

作者: 灰溜溜的小王子 | 来源:发表于2020-06-30 00:34 被阅读0次
    原因:

    NSArray/NSMutableArray 是抽象类,使用类簇创建,真正的实现类是__NSArrayI/__NSArrayM。

    例子

    为了避免数组越界,使用Runtime交换NSArray的objectAtIndex:方法到我们定义好判断的swiObjectAtIndex:方法。

    意义:数组越界的判断逻辑集中到一个地方,减少代码量。

    这是NSArray分类的逻辑,主要功能也只有一个,objectAtIndex:和swiObjectAtIndex:交换。

    + (void)load{
      [[self class] swizzlingMethodWithCurrentClass:NSClassFromString(@"__NSArrayI")
                                  OrigSelector:@selector(objectAtIndex:)
                               SwizzleSelector:@selector(swiObjectAtIndex:)];
    }
    
    - (id)swiObjectAtIndex:(NSInteger)index
    {
     if (index >= 0 && index < self.count)
    {
          return [self swiObjectAtIndex:index];
    }
         NSLog(@"index is outof array!!");
      return nil;
    }
    
     + (void)swizzlingMethodWithCurrentClass:(Class)currentClass
                       OrigSelector:(SEL)origSelector
                    SwizzleSelector:(SEL)swizzleSelector
    { Method originalMethod = class_getInstanceMethod(currentClass, origSelector);
       Method swizzleMethod = class_getInstanceMethod(currentClass, swizzleSelector);
    
       // 首先检查原方法是否存在
     BOOL didAddMethod = class_addMethod(currentClass, origSelector, method_getImplementation(swizzleMethod), method_getTypeEncoding(swizzleMethod));
    
     if (didAddMethod) //原方法不存在,需要先添加方法,再替换。class_replaceMethod 等价于class_addMethod和method_setImplementation
      {
       class_replaceMethod(currentClass, swizzleSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
      }
      else // 原方法存在,直接替换
      {
       method_exchangeImplementations(originalMethod, swizzleMethod);
      }
    }
    

    注意:
    1、方法交换的时候,使用的是NSClassFromString(@"__NSArrayI")而不是NSArray,其实最终执行方法的是__NSArrayI。NSMutableArray对应__NSArrayM。

    2、如何得知__NSArrayI/__NSArrayM类型。对!让它崩溃一次,lldb中reason会打印出来。

    参考:解释得很细致的交换方法
    交换方法的各种类型

    相关文章

      网友评论

          本文标题:对NSArray或者NSMutableArray的方法交换的问题

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