OC运行时

我个人对OC的理解就是:动态获取OC Class的属性和方法,从而动态改变Class方法和属性。
之前学习Java的时候有接触过Java的反射,感觉和OC的运行时有点相似。
下面列举一些我已经接触过OC运行时的例子运用。

1,给分类添加属性

OC分类不能添加属性,如果你在分类中添加了属性,编译器就会报警告。(记得导入 #import <objc/runtime.h>)

#import <Foundation/Foundation.h>

@interface NSObject (TBRunTimeGit)

@property (nonatomic, copy)NSString *tbName;

@end

664178F0-FBFA-4267-81D1-5CC6FA547435.png

这时,你要为分类添加属性,就用运用到OC的运行时来解决。

// 设置关联
objc_setAssociatedObject(id _Nonnull object, const void * _Nonnull key,
                         id _Nullable value, objc_AssociationPolicy policy)
// 通过关联获取属性
objc_getAssociatedObject(id _Nonnull object, const void * _Nonnull key)
    OBJC_AVAILABLE(10.6, 3.1, 9.0, 1.0, 2.0);

2,利用运行时增删改查属性/方法

1,获取类所有属性

// 获取类属性 方法1
- (NSArray *)getProperties:(id)object {
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList([object class], &count);
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0 ; i < count; i++) {
        objc_property_t t = properties[I];
        // 获取属性名
        const char * propertyName = property_getName(t);
        NSString *stringName = [NSString stringWithUTF8String:propertyName];
        NSLog(@"propertyName -- > %@", stringName);
        
        NSLog(@"%@", [NSString stringWithUTF8String:property_getAttributes(t)]);
        
        [array addObject:stringName];
    }
    return array;
}

或者

// 获取类属性 方法2
- (NSArray *)getProperties2:(id)object {
    NSMutableArray *array = [NSMutableArray array];
    unsigned int count;
    // 获取该类的所有ivar
    Ivar *ivarList = class_copyIvarList([object class], &count);
    for (int i = 0; i < count; i++) {
        Ivar ivar = ivarList[i];
        const char *ivarName = ivar_getName(ivar);
        NSString *ivarStringName = [NSString stringWithUTF8String:ivarName];
        NSLog(@"ivarStringName %@", ivarStringName);
        NSLog(@"ivar_getTypeEncoding %@", [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)]);
        [array addObject:ivarStringName];
    }
    return array;
}

2,添加属性

// 添加属性 方法1
- (void)addProperty:(id)object {
    const char *name2 = "name2";
    objc_property_attribute_t t1 = {"T", "@\"NSString\""};
    objc_property_attribute_t t2 = {"C", ""};
    objc_property_attribute_t t3 = { "V", "tangbin2" };
    objc_property_attribute_t attrs[] = {t1, t2, t3};
    // 添加属性
    class_addProperty([object class], name2, attrs, 3);
}

3,替换属性

// 替换属性
- (void)replaceProperty:(id)object {
    const char *age = "age";
    objc_property_attribute_t t1 = {"T", "@\"NSString\""};
    objc_property_attribute_t t2 = {"C", ""};
    objc_property_attribute_t t3 = { "V", "tangbin2" };
    objc_property_attribute_t attrs[] = {t1, t2, t3};
    class_replaceProperty([object class], age, attrs, 3);
}

4,获取属性

// 获取值属性
- (void)getPropertyValue:(id)object {
    Ivar ivar = class_getInstanceVariable([object class], "_name");
    NSString *name = object_getIvar(self.person, ivar);
    NSLog(@"%@", name);
}

5,修改属性值

// 修改属性
- (void)editValueIvar:(id)object {
    Ivar ivar = class_getInstanceVariable([object class], "_name");
    object_setIvar(object, ivar, @"hello China");
}

以上应用场景:修改类的私有属性值

3,黑魔法

交换函数的实现,包括类函数和实例函数

+ (void)load {
    [super load];
    Method methodSource = class_getInstanceMethod([self class], @selector(description));
    Method methodDes = class_getInstanceMethod([self class], @selector(myDescription));
    method_exchangeImplementations(methodSource, methodDes);
}

- (void)exchangeFunc {
    [self description];
}

- (NSString *)description {
    NSLog(@"%s", __func__);
    return [super description];
}

- (NSString *)myDescription {
    NSLog(@"%s", __func__);
    NSLog(@"begin");
    NSString *stringOld = [self myDescription];
    NSLog(@"%@", stringOld);
    NSLog(@"end");
    return stringOld;
}

我看到黑魔法使用案例:在MJReresh框架里面,替换UISrollView的reloadData函数,在调用系统的reloadData方法之后执行mj_reloadDataBlock
详情可看#import "UIScrollView+MJRefresh.h" 第17-25行
附上MJ大神黑魔法代码:

+ (void)exchangeInstanceMethod1:(SEL)method1 method2:(SEL)method2
{
    method_exchangeImplementations(class_getInstanceMethod(self, method1), class_getInstanceMethod(self, method2));
}

+ (void)exchangeClassMethod1:(SEL)method1 method2:(SEL)method2
{
    method_exchangeImplementations(class_getClassMethod(self, method1), class_getClassMethod(self, method2));
}
```
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容