zoukankan      html  css  js  c++  java
  • ios runtime swizzle


    ios runtime swizzle



    @implementation NSObject(Extension) + (void)swizzleClassMethod:(Class)class originSelector:(SEL)originSelector otherSelector:(SEL)otherSelector { Method otherMehtod = class_getClassMethod(class, otherSelector); Method originMehtod = class_getClassMethod(class, originSelector); // 交换2个方法的实现 method_exchangeImplementations(otherMehtod, originMehtod); } + (void)swizzleInstanceMethod:(Class)class originSelector:(SEL)originSelector otherSelector:(SEL)otherSelector { Method otherMehtod = class_getInstanceMethod(class, otherSelector); Method originMehtod = class_getInstanceMethod(class, originSelector); // 交换2个方法的实现 method_exchangeImplementations(otherMehtod, originMehtod); } @end @implementation NSArray(Extension) + (void)load { [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayI") originSelector:@selector(objectAtIndex:) otherSelector:@selector(hm_objectAtIndex:)]; } - (id)hm_objectAtIndex:(NSUInteger)index { if (index < self.count) { return [self hm_objectAtIndex:index]; } else { return nil; } } @end @implementation NSMutableArray(Extension) + (void)load { [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayM") originSelector:@selector(addObject:) otherSelector:@selector(hm_addObject:)]; [self swizzleInstanceMethod:NSClassFromString(@"__NSArrayM") originSelector:@selector(objectAtIndex:) otherSelector:@selector(hm_objectAtIndex:)]; } - (void)hm_addObject:(id)object { if (object != nil) { [self hm_addObject:object]; } } - (id)hm_objectAtIndex:(NSUInteger)index { if (index < self.count) { return [self hm_objectAtIndex:index]; } else { return nil; } } @end
  • 相关阅读:
    transform.rotation和GetComponent<Rigidbody>().MoveRotation
    indexes和indices的区别
    AnimationState
    计算边缘光照
    Marshal.FreeHGlobal 方法 (IntPtr)
    切线空间(Tangent Space)
    Unity3D中使用Profiler精确定位性能热点的优化技巧
    最美的数学定理
    [唐诗]190襄阳歌-李白
    [唐诗]189长相思-李白
  • 原文地址:https://www.cnblogs.com/stevenwuzheng/p/5493355.html
Copyright © 2011-2022 走看看