12345678910111213141516171819202122232425262728293031 |
- //
- // NSObject+Swizzling.m
- // assemble
- //
- // Created by apple on 2020/11/2.
- // Copyright © 2020 kingsunsoft. All rights reserved.
- //
- #import "NSObject+Swizzling.h"
- #import <objc/runtime.h>
- @implementation NSObject (Swizzling)
- + (void)exchangeInstanceMethodWithSelfClass:(Class)selfClass
- originalSelector:(SEL)originalSelector
- swizzledSelector:(SEL)swizzledSelector {
-
- Method originalMethod = class_getInstanceMethod(selfClass, originalSelector);
- Method swizzledMethod = class_getInstanceMethod(selfClass, swizzledSelector);
- BOOL didAddMethod = class_addMethod(selfClass,
- originalSelector,
- method_getImplementation(swizzledMethod),
- method_getTypeEncoding(swizzledMethod));
- if (didAddMethod) {
- class_replaceMethod(selfClass,
- swizzledSelector,
- method_getImplementation(originalMethod),
- method_getTypeEncoding(originalMethod));
- } else {
- method_exchangeImplementations(originalMethod, swizzledMethod);
- }
- }
- @end
|