美文网首页
iOS runtime (一)

iOS runtime (一)

作者: CaptainRoy | 来源:发表于2019-05-30 19:13 被阅读0次

    参考连接 https://www.jianshu.com/p/88d11bb12ba1

    获取类的属性

    • 新建一个 Person 类
    @interface Person : NSObject
    
    @property(nonatomic,copy)NSString *name;
    @property(nonatomic,assign)NSInteger age;
    @property(nonatomic,assign)NSInteger weight;
    
    @end
    

    然后在需要使用runtime的地方导入 #import <objc/runtime.h>

    Person *person = [[Person alloc] init];
    NSLog(@" %p ",person);
    NSLog(@" %p ",[person class]);
    NSLog(@" %p ",[person class]);
    NSLog(@" %p ",object_getClass(person));
    NSLog(@" %p ",object_getClass([Person class]));
    NSLog(@" %p ",object_getClass([person class]));
    

    打印:

    0x10064d940
    0x100002428
    0x100002428
    0x100002428
    0x100002400
    0x100002400
    
    • class_copyPropertyList 获取类的属性集合
    • property_getName 获取属性的名称
    • property_getAttributes 获取属性的真实名字和类型
    unsigned int count = 0;
    // class_copyPropertyList 获取类的属性集合
    objc_property_t *properties = class_copyPropertyList([Person class], &count);
    NSLog(@" 属性的个数 - %u ",count);
    
    for (int i = 0; i < count; i++) {
        NSString *name = [NSString stringWithFormat:@"%s",property_getName(properties[i])]; // property_getName 获取属性的名称
        NSString *attribute = [NSString stringWithFormat:@"%s",property_getAttributes(properties[i])]; // property_getAttributes 获取属性的真实名字和类型
        NSLog(@" 属性名字 - %@ ",name);
        NSLog(@" %@ ",attribute);
    }
    

    打印结果:

    2019-05-30 19:07:19.250977+0800 RunTime[1016:47491]  属性的个数 - 3 
    2019-05-30 19:07:19.251527+0800 RunTime[1016:47491]  属性名字 - name 
    2019-05-30 19:07:19.251641+0800 RunTime[1016:47491]  T@"NSString",C,N,V_name
    2019-05-30 19:07:19.251716+0800 RunTime[1016:47491]  属性名字 - age 
    2019-05-30 19:07:19.251769+0800 RunTime[1016:47491]  Tq,N,V_age
    2019-05-30 19:07:19.251822+0800 RunTime[1016:47491]  属性名字 - weight 
    2019-05-30 19:07:19.251841+0800 RunTime[1016:47491]  Tq,N,V_weight
    

    调用类的一些不公开的方法

    • .m 文件中实现如下方法
    #import "Person.h"
    
    @implementation Person
    
    // 吃饭的实例方法
    -(void)eat
    {
        NSLog(@"person eat");
    }
    
    // 吃饭的类方法
    +(void)eat
    {
        NSLog(@"Person Class eat");
    }
    
    // 睡觉实例方法
    -(void)sleepOfHour:(NSNumber *)hour
    {
        NSLog(@"person sleep hour is %@",hour);
    }
    
    // 睡觉的实例方法
    +(void)sleepOfHour:(NSNumber *)hour
    {
        NSLog(@"Person Class sleep hour is %@",hour);
    }
    
    // 是否吃饱
    -(NSNumber *)eatEnough:(NSNumber *)breadCount
    {
        NSLog(@"person eat breadCount is %@",breadCount);
        return breadCount;
    }
    
    +(NSNumber *)eatEnough:(NSNumber *)breadCount
    {
        NSLog(@"Person Class eat breadCount is %@",breadCount);
        return breadCount;
    }
    
    • 导入 #import <objc/runtime.h> 和 #import <objc/message.h>
    Person *person = [[Person alloc] init];
    
    ((void (*)(id,SEL)) objc_msgSend)(person,NSSelectorFromString(@"eat"));
    ((void (*)(id,SEL)) objc_msgSend)([Person class],NSSelectorFromString(@"eat"));
    
    ((void (*)(id,SEL,NSNumber *)) objc_msgSend)(person,NSSelectorFromString(@"sleepOfHour:"),@2);
    ((void (*)(id,SEL,NSNumber *)) objc_msgSend)([Person class],NSSelectorFromString(@"sleepOfHour:"),@3);
    
    NSNumber *personBreadCount = ((NSNumber * (*)(id,SEL,NSNumber *)) objc_msgSend)(person,NSSelectorFromString(@"eatEnough:"),@4);
    NSLog(@"personBreadCount - %@",personBreadCount);
    
    NSNumber *PersonBreadCount = ((NSNumber * (*)(id,SEL,NSNumber *)) objc_msgSend)([Person class],NSSelectorFromString(@"eatEnough:"),@5);
    NSLog(@"PersonBreadCount - %@",PersonBreadCount);
    

    打印:

    2019-05-30 19:11:40.644604+0800 RunTime[1035:48715] person eat
    2019-05-30 19:11:40.644860+0800 RunTime[1035:48715] Person Class eat
    2019-05-30 19:11:40.644950+0800 RunTime[1035:48715] person sleep hour is 2
    2019-05-30 19:11:40.645014+0800 RunTime[1035:48715] Person Class sleep hour is 3
    2019-05-30 19:11:40.645051+0800 RunTime[1035:48715] person eat breadCount is 4
    2019-05-30 19:11:40.645073+0800 RunTime[1035:48715] personBreadCount - 4
    2019-05-30 19:11:40.645092+0800 RunTime[1035:48715] Person Class eat breadCount is 5
    2019-05-30 19:11:40.645139+0800 RunTime[1035:48715] PersonBreadCount - 5
    

    相关文章

      网友评论

          本文标题:iOS runtime (一)

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