参考方法链接
runtime实战应用
神经病院Objective-C Runtime入院
相关定义
typedef struct objc_method *Method; ///< 描述类中的一个方法
typedef struct objc_ivar *Ivar; ///< 实例变量
typedef struct objc_category *Category; ///< 类别Category
typedef struct objc_property *objc_property_t;///< 类中声明的属性
- 类在runtime中的表示
//类在runtime中的表示
struct objc_class {
Class isa; //指针
//实例的isa指向类对象,类对象的isa指向元类
//对象的实例方法调用时,通过对象的 isa 在类中获取方法的实现。
//类对象的类方法调用时,通过类的 isa 在元类中获取方法的实现。
#if !__OBJC2__
Class super_class; //指向父类
const char *name; //类名
long version;
long info;
long instance_size
struct objc_ivar_list *ivars //成员变量列表
struct objc_method_list **methodLists; //方法列表
struct objc_cache *cache;//缓存
//一种优化,调用过的方法存入缓存列表,下次调用先找缓存
struct objc_protocol_list *protocols //协议列表
#endif
} OBJC2_UNAVAILABLE;
/* Use `Class` instead of `struct objc_class *` */
常见方法
unsigned int count;
- 获取属性列表
objc_property_t *propertyList = class_copyPropertyList([self class], &count);
for (unsigned int i = 0; i < count; i++){
const char *propertyName = property_getName(propertyList[i]);
NSLog(@"property---->%@", [NSString stringWithUTF8String:propertyName]);
}
- 获取方法列表
Method *methodList = class_copyMethodList([self class], &count);
for (unsigned int i = 0; i<count; i++) {
Method method = methodList[i];
IMP imp = method_getImplementation(method);
SEL sel = method_getName(method);
unsigned int argumentCount = method_getNumberOfArguments(method);
NSLog(@"method---->%@", sel_getName(_sel)));
}
- 获取成员变量列表
Ivar *ivarList = class_copyIvarList([self class], &count);
for (unsigned int i = 0; i<count; i++) {
Ivar myIvar = ivarList[i];
const char *ivarName = ivar_getName(myIvar);
NSLog(@"Ivar---->%@", [NSString stringWithUTF8String:ivarName]);
}
- 获取协议列表
//获取协议列表
__unsafe_unretained Protocol **protocolList = class_copyProtocolList([self class], &count);
for (unsigned int i = 0; i<count; i++) {
Protocol *myProtocal = protocolList[i];
const char *protocolName = protocol_getName(myProtocal);
NSLog(@"protocol---->%@", [NSString stringWithUTF8String:protocolName]);
}
- 发送消息
objc_msgSend
Student *stu = [Student alloc] init]; ///< 创建Student 对象
[stu run]; ///< 调用student方法
objc_msgSend(p, @selector(run));///< 本质 让对象发送消息
// 调用类方法的方式:两种
// 第一种通过类名调用
[Student run];
// 第二种通过类对象调用
[[Student class] run];
// 本质 让类对象发送消息
objc_msgSend([Student class], @selector(run));
- 交换方法
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//case1: 替换实例方法
Class selfClass = [self class];
//case2: 替换类方法
Class selfClass = object_getClass([self class]);
//源方法的SEL和Method
SEL oriSEL = @selector(viewWillAppear:);
Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
//交换方法的SEL和Method
SEL cusSEL = @selector(customViewWillApper:);
Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
//先尝试給源方法添加实现,这里是为了避免源方法没有实现的情况
BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
if (addSucc) {
//添加成功:将源方法的实现替换到交换方法的实现
class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
}else {
//添加失败:说明源方法已经有实现,直接将两个方法的实现交换即可
method_exchangeImplementations(oriMethod, cusMethod);
}
});
swizzing 封装
+ (void)swizzleMethods:(Class)class originalSelector:(SEL)origSel swizzledSelector:(SEL)swizSel {
Method origMethod = class_getInstanceMethod(class, origSel);
Method swizMethod = class_getInstanceMethod(class, swizSel);
BOOL didAddMethod = class_addMethod(class, origSel, method_getImplementation(swizMethod), method_getTypeEncoding(swizMethod));
if (didAddMethod) {
class_replaceMethod(class, swizSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
} else {
method_exchangeImplementations(origMethod, swizMethod);
}
}
- 拦截方法
动态添加方法
+ (BOOL)resolveClassMethod:(SEL)sel;
+ (BOOL)resolveInstanceMethod:(SEL)sel;
//后两个方法需要转发到其他的类处理
- (id)forwardingTargetForSelector:(SEL)aSelector;
- (void)forwardInvocation:(NSInvocation *)anInvocation;