Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
通过 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