Created
March 7, 2018 07:09
-
-
Save juwencheng/15f714148339250b40c1c3d397a88e7a to your computer and use it in GitHub Desktop.
通过 resolveInstanceMethod: 解决无法找到selector导致的奔溃。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
@interface MisImp : NSObject | |
- (void)foo; | |
+ (void)fooMethod; | |
@end | |
#import "MisImp.h" | |
#import <objc/runtime.h> | |
void dynamicMethodIMP(id self, SEL _cmd) { | |
NSLog(@"😄 dynamicMethodIMP"); | |
} | |
void classDynamicMethodIMP(id self, SEL _cmd) { | |
NSLog(@"😄 classDynamicMethodIMP"); | |
} | |
@implementation MisImp | |
+ (BOOL)resolveInstanceMethod:(SEL)sel { | |
if (sel == @selector(foo)) { | |
class_addMethod([self class], sel, (IMP)dynamicMethodIMP, "v@:"); | |
return YES; | |
} | |
return [super resolveInstanceMethod:sel]; | |
} | |
+ (BOOL)resolveClassMethod:(SEL)sel{ | |
Class metaClass = objc_getMetaClass(class_getName(self)); | |
if (sel == @selector(fooMethod)) { | |
class_addMethod(metaClass, sel, (IMP)classDynamicMethodIMP, "v@:"); | |
return YES; | |
} | |
return [super resolveClassMethod:sel]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment