美文网首页
iOS  Method Swizzling for NSArra

iOS  Method Swizzling for NSArra

作者: 关灯侠 | 来源:发表于2017-02-28 00:28 被阅读394次

背景:直接 对NSArray/NSMutableArray的方法交换时,总是不成功

原因

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

例子

为了避免数组越界,使用Runtime交换NSArrayobjectAtIndex:方法到我们定义好判断的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);
   }
}

最终使用的时候,就不会崩溃了。

NSArray *array = @[@"1",@"2"];
NSString *str = [array objectAtIndex:4];
NSLog(@"str --- %@",str);

注意:

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

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

01.png

参考:
http://www.jianshu.com/p/f58a9e4be184
解释得很细致的交换方法
交换方法的各种类型

相关文章

网友评论

      本文标题:iOS  Method Swizzling for NSArra

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