您的位置:首页 > 移动开发 > IOS开发

Runtime运行时的简单使用,字典转模型

2016-12-05 11:27 357 查看
自我感觉挺简单的,适合初学者

、、、

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    //获取person类额属性

    //使用runtime获取属性列表

    NSArray *properties = [Person zl_objecProperties];

    

    NSLog(@"%@",properties);

    

    //字典转模型

    Person *person = [Person zl_objcWithDictionary:@{@"name":@"zhangsan",@"age":@23,@"title":@"xigua",@"height":@21}];

    

    NSLog(@"%@",person);

    

}

、、、

模型类中

、、、

#import <Foundation/Foundation.h>

@interface Person : NSObject

//@property(nonatomic,strong) NSString *name;

//@property(nonatomic,assign) NSInteger age;

//@property(nonatomic,strong) NSString *title;

//@property(nonatomic,assign) double height;

@end

、、、

、、、

#import "Person.h"

@interface Person ()

@property(nonatomic,strong) NSString *name;

@property(nonatomic,assign) NSInteger age;

@property(nonatomic,strong) NSString *title;

@property(nonatomic,assign) double height;

@end

@implementation Person

- (NSString *)description

{

    

    NSArray *key = @[@"name",@"age",@"title",@"height"];

    return [[self dictionaryWithValuesForKeys:key]description];

}

@end

、、、

解析的工具类

、、、

#import <Foundation/Foundation.h>

#import <objc/runtime.h>

@interface NSObject (runtime)

//获取累的属性列表

+ (NSArray *)zl_objecProperties;

//字典转模型

+(instancetype)zl_objcWithDictionary:(NSDictionary *)dic;

@end

、、、

、、、

#import "NSObject+runtime.h"

@implementation NSObject (runtime)

+ (instancetype)zl_objcWithDictionary:(NSDictionary *)dic

{

    

    id obc = [[self alloc]init];

    NSArray *properList = [self zl_objecProperties];

    [dic enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

       

        //判断模型中是否有key的属性

        if ([properList containsObject:key])

        {

            [obc setValue:obj forKey:key];

        }

    }];

    return obc;

}

const char *ZLProperLsitKey = "ZLProperLsitKey";

+ (NSArray *)zl_objecProperties

{

    

    /**

     获取关联对象 -- 动态增加属性

     */

    NSArray *prtList = objc_getAssociatedObject(self, ZLProperLsitKey);

    if (prtList != nil)

    {

        //不用每次调用运行时方法 提高效率

        return prtList;

    }

    

    //获取属性列表

    unsigned int count = 0;

    objc_property_t *properList = class_copyPropertyList([self class], &count);

    

    NSLog(@"%d",count);

    

    NSMutableArray *array = [NSMutableArray array];

    for (unsigned i = 0; i < count;i++)

    {

        //从数组中取得属性

        //上面是数组所以有*

        objc_property_t pry = properList[i];

        

        const char *name = property_getName(pry);

        

        [array addObject:[NSString stringWithFormat:@"%s",name]];

        

//        [array addObject:[NSString stringWithCString:name encoding:NSUTF8StringEncoding]];

        

    }

    //c语言需要释放

    free(properList);

    

    //到此为止获取属性列表结束  领用关联对象,动态增加属性

    objc_setAssociatedObject(self, ZLProperLsitKey, array.copy, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    

    return array;

}

@end

、、、
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios