Objective-C运行时method_exchangeImplementations无效

时间:2015-09-09 10:54:42

标签: ios objective-c runtime

下面是我的代码,请帮我查一下这个错误的原因是什么。 错误是

  

xxx_objectAtIndex :( NSUInteger)索引无法正常工作

代码:

+ (void)load 
{
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    Class class = [self class];
    /* When swizzling a class method, use the following: */
    /* Class class = object_getClass((id)self); */

    SEL originalSelector = @selector(objectAtIndex:);/* NSArray's way */
    SEL swizzledSelector = @selector(xxx_objectAtIndex:);/* will change way */

    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    method_exchangeImplementations(originalMethod, swizzledMethod);


  });
}
/*but when I use objectAtIndex: not perform this way*/
- (id)xxx_objectAtIndex:(NSUInteger)index
{

   NSLog(@"=========1===========");
   if (index < self.count)
   {
       return [self xxx_objectAtIndex:index];
   }

   return @"";
}

0 个答案:

没有答案