Skip to content

Instantly share code, notes, and snippets.

@juwencheng
Created March 7, 2018 07:09
Show Gist options
  • Save juwencheng/15f714148339250b40c1c3d397a88e7a to your computer and use it in GitHub Desktop.
Save juwencheng/15f714148339250b40c1c3d397a88e7a to your computer and use it in GitHub Desktop.
通过 resolveInstanceMethod: 解决无法找到selector导致的奔溃。
#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