dkahgld

NSObject+FMSwizzling.m 1.2KB

1234567891011121314151617181920212223242526272829303132
  1. //
  2. // NSObject+FMSwizzling.m
  3. // FMListPlaceholderExample
  4. //
  5. // Created by Mingo on 2018/8/9.
  6. // Copyright © 2017年 袁凤鸣. All rights reserved.
  7. // 项目地址:https://github.com/yfming93/FMListPlaceholder
  8. // 作者博客:https://www.yfmingo.cn
  9. //
  10. #import "NSObject+FMSwizzling.h"
  11. @implementation NSObject (FMSwizzling)
  12. + (void)fm_methodSwizzlingWithOriginalSelector:(SEL)originalSelector
  13. bySwizzledSelector:(SEL)swizzledSelector {
  14. Class class = [self class];
  15. Method originalMethod = class_getInstanceMethod(class, originalSelector);
  16. Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
  17. BOOL didAddMethod = class_addMethod(class,originalSelector,
  18. method_getImplementation(swizzledMethod),
  19. method_getTypeEncoding(swizzledMethod));
  20. if (didAddMethod) {
  21. class_replaceMethod(class,swizzledSelector,
  22. method_getImplementation(originalMethod),
  23. method_getTypeEncoding(originalMethod));
  24. } else {
  25. method_exchangeImplementations(originalMethod, swizzledMethod);
  26. }
  27. }
  28. @end