美文网首页
Runtime使用总结

Runtime使用总结

作者: 卡丁车手 | 来源:发表于2017-06-25 10:05 被阅读36次

    本文主要内容有:获取属性/方法/协议/成员变量列表、动态关联属性、动态添加方法、方法交换。

    一、获取列表

    - (void)getList {
        unsigned int count;
        // 获取属性列表
        objc_property_t *propertyList = class_copyPropertyList([self class], &count);
        for (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 (int i = 0; i < count; i++) {
            Method method = methodList[i];
            NSLog(@"method-->%@", NSStringFromSelector(method_getName(method)));
        }
    
        // 获取成员变量列表
        Ivar *ivarList = class_copyIvarList([self class], &count);
        for (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 (int i = 0; i < count; i++) {
            Protocol *myProtocal = protocolList[i];
            const char *protocolName = protocol_getName(myProtocal);
            NSLog(@"protocol-->%@", [NSString stringWithUTF8String:protocolName]);
        }
    }
    

    使用Runtime可以获取一个类的所有成员变量,使用KVC可以修改变量的值,所以在OC中没有真正的私有变量;所有的方法也可以通过Runtime获取并调用,所以OC中也没有真正的私有方法。

    MJExtension、YYModel等字典转模型库,原理都是使用Runtime获取属性列表,然后通过KVC进行赋值。

    二、动态关联属性

    .h文件

    #import "Lender.h"
    
    @interface Lender (Category)
    @property (nonatomic, copy) NSString *categoryName;
    
    @end
    

    .m文件
    #import "Lender+Category.h"
    #import <objc/runtime.h>

    static char category_Name;
    
    @implementation Lender (Category) 
    
    - (void)setCategoryName:(NSString *)categoryName {
        objc_setAssociatedObject(self, &category_Name, categoryName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSString *)categoryName {
        return objc_getAssociatedObject(self, &category_Name);
    }
    
    @end
    

    动态关联属性常用在分类中,由于分类中添加属性并不能自动生成成员变量,setter/getter方法也就失去了意义,使用动态关联属性可以给分类添加“有意义”的属性。

    不过,动态关联的属性只有5种状态:
    OBJC_ASSOCIATION_ASSIGN
    OBJC_ASSOCIATION_RETAIN_NONATOMIC
    OBJC_ASSOCIATION_COPY_NONATOMIC
    OBJC_ASSOCIATION_RETAIN
    OBJC_ASSOCIATION_COPY
    retain可以当作strong来用,assign和copy都有,可是我们常用的weak却没有。

    我们知道,weak一般用于会发生循环引用的地方,使用weak表示弱引用,一旦对象没有强引用指针被释放,weak指针也随之置为nil,这样就非常安全。如果是assign指针,虽然也是弱引用,但是对象释放时不会自动置nil,会出现野指针,这时再给它发消息程序就会崩溃,而给空指针发消息是不会崩溃的。

    那如果想给分类添加一个weak属性该怎么做呢?具体可以参考博客《如何使用 Runtime 给现有的类添加 weak 属性》

    三、动态添加方法

    - (void)addMethod {
        SEL methodSel = @selector(myInstanceMethod:);
        Method method = class_getInstanceMethod([self class], methodSel);
        IMP imp = method_getImplementation(method);
        const char *types = method_getTypeEncoding(method);
        class_addMethod([self class], methodSel, imp, types);
    }
    
    - (void)myInstanceMethod:(NSString *)sender {
        NSLog(@"myInstanceMethod:%@", sender);
    }
    

    动态添加方法一般用于消息转发和方法交换。

    四、方法交换(Method Swizzling)

    + (void)load {
        SEL originalSelector = @selector(originalMethod);
        SEL overrideSelector = @selector(replace_originalMethod);
        Method originalMethod = class_getInstanceMethod(self, originalSelector);
        Method overrideMethod = class_getInstanceMethod(self, overrideSelector);
    
        if (class_addMethod(self, originalSelector, method_getImplementation(overrideMethod), method_getTypeEncoding(overrideMethod))) {
            class_replaceMethod(self, overrideSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
        } else {
            //添加失败了 说明本类中有methodB的实现,此时只需要将methodA和methodB的IMP互换一下即可
            method_exchangeImplementations(originalMethod, overrideMethod);
        }
    }
    

    未完待续

    相关文章

      网友评论

          本文标题:Runtime使用总结

          本文链接:https://www.haomeiwen.com/subject/rfmkcxtx.html