(一)分类中调用主类的实例方法或者类方法
场景:SDK中有个打点方法,但只有.h,没有.m实现方法,现在需求是要对所有打点增加一个userName参数。方法一是在每个调用处都手动添加,这样得改几十处的代码,并且以后也得显性增加这个参数,明显很麻烦;第二个是利用分类 覆盖这个方法,在这个方法中统一加userName参数,追加完后再调用原类的方法,如此原来的调用不需要一一加参数修改。
#import "LoadViewController.h"
NS_ASSUME_NONNULL_BEGIN
@interface LoadViewController (Extension)
@end
NS_ASSUME_NONNULL_END
#import "LoadViewController+Extension.h"
#include <objc/runtime.h>
@implementation LoadViewController (Extension)
//在分类中直接覆写原类的方法
+ (void)setCustomEvent:(NSString *)eventId withLabel:(NSString *)eventLabel parameters:(NSDictionary *)parameters :(NSString*)num {
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithDictionary:parameters];
[mutableDic setObject:@"42561324" forKey:@"userName"];
parameters = [mutableDic copy];
//通过类对象获取元类对象
Class metaClass = object_getClass([LoadViewController class]);
unsigned int count;
Method *classMethods = class_copyMethodList(metaClass, &count); // 获取所有类方法列表,metaClass换成[self class]就获取所有实例方法列表
for ( int i = count - 1 ; i >= 0; i--) { // 从后往前遍历,因为分类方法被放在了主类方法前
Method method = classMethods[I];
SEL name = method_getName(method); // 方法选择器
IMP imp = method_getImplementation(method); // 方法实现
if (name == _cmd) { //等于当前方法
//使用函数指针调用,(void *)只是告诉编译器,不用报类型强转的warning
void(*function)(id, SEL, NSString *,NSString *,NSDictionary *,NSString *) = (void *)imp;
function([LoadViewController class],name,eventId,eventLabel,parameters,num);
break;
}
}
free(classMethods);
}
(二)class类结构体:
看到里面有一个struct objc_method_list ** methodLists
,这就是存储该类所有方法的地方了。查找方法的时候并不是每次都去遍历methodList的,而是先去cache中查,cache中存储了最近常用的方法。
看一下objc_method_list这个结构体
它有一个指向存储废弃方法列表的指针struct objc_method_list *obsolete,还有方法的个数int method_count,还有一个用于存储方法的数组struct objc_method method_list[1]。其中数组的长度是可变的。
看一下objc_method这个结构体,
SEL method_name表示方法名,char *method_types表示参数及返回值类型,IMP method_imp表示指向方法实现的指针。
(三)、runtime 获取类的属性,实例方法,类方法和IMP交换等
导入:
#import <objc/message.h>
协议方法的获取
- (NSArray<NSString *> *)methodListWithProtocol:(Protocol *)protocol {
unsigned int count = 0;
NSMutableArray<NSString *> *methodList = @[].mutableCopy;
struct objc_method_description *methods = protocol_copyMethodDescriptionList(protocol, YES, YES, &count);
for (unsigned int i = 0; i < count; i++) {
struct objc_method_description method = methods[I];
NSString *name = NSStringFromSelector(method.name);
[methodList addObject:name];
}
free(methods);
return methodList;
}
类属性的获取
- (void)getIvarAndChange {
NSLog(@"修改前:%@", self.name);
unsigned int count = 0;
Ivar *members = class_copyIvarList([self class], &count);
for(int i = 0; i < count; i++) {
Ivar ivar = members[I];
const char *memberName = ivar_getName(ivar);
const char *memberType = ivar_getTypeEncoding(ivar);
//依次打印属性名称和属性类型
NSLog(@"%s : %s", memberName, memberType);
if(strcmp(memberName, "_name") == 0) {
// 修改前
NSString *name = (NSString *)object_getIvar(self, members[I]);
NSLog(@"-name:%@", name);
// 修改后
object_setIvar(self, members[i], @"test");
NSString *nameReset = (NSString *)object_getIvar(self, members[I]);
NSLog(@"-nameReset:%@", nameReset);
break;
}
}
free(members);
NSLog(@"修改后:%@", self.name);
}
实例方法的获取
- (void)getMethod {
unsigned int count;
Method *methods = class_copyMethodList([self class], &count);
for (int i = 0; i < count; i++) {
Method method = methods[I];
SEL selector = method_getName(method);
NSString *name = NSStringFromSelector(selector);
NSLog(@"实例方法:%@",name);
}
free(methods);
}
类方法的获取
- (void)getClassMethod {
Class metaClass = object_getClass([self class]);
unsigned int count;
Method *classMethods = class_copyMethodList(metaClass, &count);
for (int i = 0; i < count; i++) {
Method classMethod = classMethods[I];
SEL selector = method_getName(classMethod);
NSString *name = NSStringFromSelector(selector);
NSLog(@"类方法:%@",name);
}
free(classMethods);
}
方法交换 method swizzled
+ (void)load{
static dispatch_once_t onceToken;
dispatch_once(&onceToken,^{
Class class = [self class];
SEL originalSelector = @selector(viewDidAppear:);
SEL mySwizzledSelector = @selector(myViewDidAppear:);
Method originalMethod = class_getInstanceMethod(class, originalSelector);
Method mySwizzledMethod = class_getInstanceMethod(class, mySwizzledSelector);
BOOL addResult = class_addMethod(class, originalSelector, method_getImplementation(mySwizzledMethod), method_getTypeEncoding(mySwizzledMethod));
if (addResult){
class_replaceMethod(class, mySwizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
}else{
method_exchangeImplementations(originalMethod, mySwizzledMethod);
}
});
}
- (void)myViewDidAppear:(BOOL)animated
{
[self myViewDidAppear:animated];
NSLog(@"-----my view Did appear----,vc name = %@",[self class]);
}