iOS-浅谈OC中Runtime的应用

2019-06-06  本文已影响0人  晴天ccc

目录


  • ---- 创建一个类
    ---- 添加成员变量、方法
    ---- 注册类
    ---- 重新设置class
    ---- 判断是否是类对象
  • 成员变量
    ---- 读取类中的成员变量信息
    ---- 设置、读取实例对象中成员变量的值
    ---- 读取类中所有成员变量信息
    ---- 读取系统类的私有变量
    ---- Json转Model
  • 方法
    ---- 替换方法实现
    ---- 交换方法实现
    ------------ 底层原理
    ------------ 交换自定义方法
    ------------ 拦截系统方法
    ------------ 崩溃防护
  • Runtime API
    ---- 类
    ---- 成员变量
    ---- 属性
    ---- 方法

Class dogClass = objc_allocateClassPair([NSObject class], "Dog", 0);
// 添加一个成员变量
class_addIvar(dogClass, "_age", 4, 1, @encode(int));

// 添加一个方法
class_addMethod(dogClass, @selector(run), (IMP)run, "V@:");
// 创建后需要注册这个类
objc_registerClassPair(dogClass);
  • 运行时可以创建一个新的类,创建类需要在调用objc_registerClassPair前添加成员变量。
    成员变量信息保存在类对象的class_ro_t中,是只读的。
  • 方法可以不用在调用objc_registerClassPair前添加,因为后添加的方法会保存到class_rw_tmethods中,是可读可写的。但是最好还是一起在注册类之前添加。
Person *person = [[Person alloc] init];
[person run];

// 修改person的类型,修改isa的指向
object_setClass(person, [Car class]);
[person run];

上面代码执行结果是person会调用Car类中的run方法。

int isClass1 = object_isClass(person);
int isClass2 = object_isClass(object_getClass(person))
int isClass3 = object_isClass(object_getClass([Person class])))
// 判断是否是类对象
NSLog(@"%d, %d, %d", isClass1, isClass2, isClass3);

打印结果:

0, 1, 1

成员变量

// 获取成员变量信息
Ivar nameIvar = class_getInstanceVariable([Person class], "_name");

// 获取成员变量的name和类型编码
NSLog(@"%s %s",ivar_getName(nameIvar), ivar_getTypeEncoding(nameIvar));
Person *person = [[Person alloc] init];

// 设置成员变量的值
object_setIvar(person, nameIvar, @"Jack");
NSLog(@"person.name = %@", object_getIvar(person, nameIvar));
// 读取所有成员变量
unsigned int count;
Ivar *ivars = class_copyIvarList([Person class], &count);
for (int i = 0; i < count; i++) {
    Ivar ivar = ivars[i];
    NSLog(@"%s %s",ivar_getName(ivar), ivar_getTypeEncoding(ivar));
}
// runtime里调用copy方法,需要手动销毁
free(ivars);

比如读取修改UITextField的placeholder颜色:

// 先读取成员变量
Ivar ivar = class_getInstanceVariable([UITextField class], "_placeholderLabel");
// 再获取成员变量的值
UILabel* label = object_getIvar(textField, ivar);
// 设置颜色
label.textColor = [UIColor redColor];

iOS13以后,通过KVC读取系统类的私有成员变量可能会失败。
例如id label = [textField valueForKey:@"_placeholderLabel"]; 会报错。

给NSObject新建一个分类,添加xl_objectWithJson方法,创建一个实例对象,获取Class中的所有成员变量,再将成员变量利用KVC添加到实例对象中:

+ (id)xl_objectWithJson:(NSDictionary *)json {
    id obj = [[self alloc] init];
    
    unsigned int count;
    Ivar *ivars = class_copyIvarList(self, &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivars[i];
        NSMutableString *ivarName = [NSMutableString stringWithUTF8String:ivar_getName(ivar)];
        [ivarName deleteCharactersInRange:NSMakeRange(0, 1)];
        [obj setValue:json[ivarName] forKey:ivarName];
    }
    return obj;
}

封装一个完整的工具还需要判断很多问题,比如Json和Model数据不匹配、Model存在继承、Model嵌套Model等。

方法

Person *person = [[Person alloc] init];

Method run = class_getInstanceMethod([Person class], @selector(run));
Method replaceRun = class_getInstanceMethod([self class], @selector(replaceRun));

class_replaceMethod([Person class], method_getName(run), method_getImplementation(replaceRun), method_getTypeEncoding(replaceRun));

替换后的效果是调用run方法时,会执行replaceRun方法里的代码。

【查看:objc源码objc-runtime-new.mm

void method_exchangeImplementations(Method m1, Method m2)
{
    if (!m1  ||  !m2) return;
    mutex_locker_t lock(runtimeLock);
    IMP imp1 = m1->imp(false);
    IMP imp2 = m2->imp(false);
    SEL sel1 = m1->name();
    SEL sel2 = m2->name();

    m1->setImp(imp2);
    m2->setImp(imp1);

    flushCaches(nil, __func__, [sel1, sel2, imp1, imp2](Class c){
        return c->cache.shouldFlush(sel1, imp1) || c->cache.shouldFlush(sel2, imp2);
    });

    adjustCustomFlagsForMethodChange(nil, m1);
    adjustCustomFlagsForMethodChange(nil, m2);
}

交换方法实现本质上就是交换了两个Method对象的IMP(实现)。交换后会清空两个类的方法缓存。

Person *person = [[Person alloc] init];

Method run = class_getInstanceMethod([Person class], @selector(run));
Method walk = class_getInstanceMethod([Person class], @selector(walk));

method_exchangeImplementations(run, walk);

交换后的效果就是调用run方法时,会执行walk方法里的代码,调用walk时,会执行run方法里的代码。

如果想拦截(hook)所有按钮的点击事件,可以交换sendAction:to:forEvent:方法,可以创建UIControl的分类,添加如下代码:

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Method method1 = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
        Method method2 = class_getInstanceMethod(self, @selector(xl_sendAction:to:forEvent:));
        method_exchangeImplementations(method1, method2);
    });
}

- (void)xl_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    
    // 调用被交换的方法
    [self xl_sendAction:action to:target forEvent:event];
    
    NSLog(@"%s", __func__);
}

拦截操作需要在自定义方法中调用原方法。交换方法时最好使用dispatch_once,保证直交换一次。

拦截系统方法可以做很多事情,比如拦截[UIFont systemFontOfSize:]方法,统一设置字体大小系数、拦截viewWillAppear:和viewWillDisappear:来处理页面统计等工作。除了拦截系统方法,也可以在分类里拦截自定义主类中的方法,帮助主类分担工作。

可以hook数组或者字典添加元素的方法,避免因为添加空数据导致的闪退。
数组可以在NSMutableArray的分类中添加如下代码:

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class arrayClass = NSClassFromString(@"__NSArrayM");
        Method method1 = class_getInstanceMethod(arrayClass, @selector(insertObject:atIndex:));
        Method method2 = class_getInstanceMethod(arrayClass, @selector(xl_insertObject:atIndex:));
        method_exchangeImplementations(method1, method2);
    });
}

- (void)xl_insertObject:(id)anObject atIndex:(NSUInteger)index {
    
    if (anObject) {
        [self xl_insertObject:anObject atIndex:index];
    }
}

字典可以在NSMutableDictionary分类中添加如下代码:

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class arrayClass = NSClassFromString(@"__NSDictionaryM");
        Method method1 = class_getInstanceMethod(arrayClass, @selector(setObject:forKey:));
        Method method2 = class_getInstanceMethod(arrayClass, @selector(xl_setObject:forKey:));
        method_exchangeImplementations(method1, method2);
    });
}

- (void)xl_setObject:(id)anObject forKey:(id<NSCopying>)aKey {
    if (anObject && aKey) {
        [self xl_setObject:anObject forKey:aKey];
    }
}

+load方法是在程序启动时调用的,如果添加过多代码可能会增加启动时间,可以用+initialize和dispatch_once结合替换+load方法。

Runtime API

动态创建一个类(参数:父类,类名,额外的内存空间)
Class objc_allocateClassPair(Class superclass, const char *name, size_t extraBytes)

注册一个类(要在类注册之前添加成员变量)
void objc_registerClassPair(Class cls) 

销毁一个类
void objc_disposeClassPair(Class cls)

获取isa指向的Class
Class object_getClass(id obj)

设置isa指向的Class
Class object_setClass(id obj, Class cls)

判断一个OC对象是否为Class
BOOL object_isClass(id obj)

判断一个Class是否为元类
BOOL class_isMetaClass(Class cls)

获取父类
Class class_getSuperclass(Class cls)
获取一个实例变量信息
Ivar class_getInstanceVariable(Class cls, const char *name)

拷贝实例变量列表(最后需要调用free释放)
Ivar *class_copyIvarList(Class cls, unsigned int *outCount)

设置和获取成员变量的值
void object_setIvar(id obj, Ivar ivar, id value)
id object_getIvar(id obj, Ivar ivar)

动态添加成员变量(已经注册的类是不能动态添加成员变量的)
BOOL class_addIvar(Class cls, const char * name, size_t size, uint8_t alignment, const char * types)

获取成员变量的相关信息
const char *ivar_getName(Ivar v)
const char *ivar_getTypeEncoding(Ivar v)
获取一个属性
objc_property_t class_getProperty(Class cls, const char *name)

拷贝属性列表(最后需要调用free释放)
objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)

动态添加属性
BOOL class_addProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
                  unsigned int attributeCount)

动态替换属性
void class_replaceProperty(Class cls, const char *name, const objc_property_attribute_t *attributes,
                      unsigned int attributeCount)

获取属性的一些信息
const char *property_getName(objc_property_t property)
const char *property_getAttributes(objc_property_t property)
获得一个实例方法、类方法
Method class_getInstanceMethod(Class cls, SEL name)
Method class_getClassMethod(Class cls, SEL name)

方法实现相关操作
IMP class_getMethodImplementation(Class cls, SEL name) 
IMP method_setImplementation(Method m, IMP imp)
void method_exchangeImplementations(Method m1, Method m2) 

拷贝方法列表(最后需要调用free释放)
Method *class_copyMethodList(Class cls, unsigned int *outCount)

动态添加方法
BOOL class_addMethod(Class cls, SEL name, IMP imp, const char *types)

动态替换方法
IMP class_replaceMethod(Class cls, SEL name, IMP imp, const char *types)

获取方法的相关信息(带有copy的需要调用free去释放)
SEL method_getName(Method m)
IMP method_getImplementation(Method m)
const char *method_getTypeEncoding(Method m)
unsigned int method_getNumberOfArguments(Method m)
char *method_copyReturnType(Method m)
char *method_copyArgumentType(Method m, unsigned int index)

选择器相关
const char *sel_getName(SEL sel)
SEL sel_registerName(const char *str)

用block作为方法实现
IMP imp_implementationWithBlock(id block)
id imp_getBlock(IMP anImp)
BOOL imp_removeBlock(IMP anImp)
上一篇 下一篇

猜你喜欢

热点阅读