美文网首页
YYmodel-数据转模型

YYmodel-数据转模型

作者: bugLife丶 | 来源:发表于2020-12-09 14:05 被阅读0次

    iOS开发比较方便的数据转模型

    刚开发做iOS开发的时候一直用MJextention,该框架底层其实是采用的runtime,也是比较优秀方便的。今天我们要说的是另外一个框架YYmodel,它同样是一个数据快速转模型的框架,它跟其他不同的是,它核心是是runtime遍历结构体的ivars的值,并且是NSObjct的category,使用起来也方便。下面我们就来简单的使用一下。

    简单的数据json、dictionary转模型

    //person对象model,student为嵌套对象
    @interface Person : NSObject
    @property(nonatomic,copy)NSString *name;
    @property(nonatomic,copy)NSString *info;
    @property(nonatomic,assign)int age;
    @property(nonatomic,strong)student *stu;
    -(void)superClass:(NSString *)name;
    @end
    //代码实现部分
      NSDictionary *testDict = @{@"name_1":@"testName",@"age_1":@(19),@"info_1":@"testInfo~~~",@"stu":@{@"name":@"gee",@"number":@"123456"}};
     Person *p = [Person modelWithDictionary:testDict];
     NSLog(@"%@-%d-%@-%@-%@",p.name,p.age,p.info,p.stu.name,p.stu.number);"%@-%d-%@",p.name,p.age,p.info);
     NSDictionary *dict = [p modelToJSONObject];
     NSLog(@"%@",dict);
    /*
    *关键代码 modelWithDictionary,`NSDictionary`传入一个字典
    * modelWithJson类似modelWithDictionary ,不一样的地方是可以传入`NSDictionary`,  *`NSString` or `NSData`
    * modelToJSONObject(modelToJSOString)为模型转json对象(字符串)
    */
    
    //以下处理模型字段跟后台返回的数据key不一致情况处理
    //自定义属性,对后台返回的字段映射
    +(NSDictionary *)modelCustomPropertyMapper{
        return @{
                 @"name":@"name_1",
                 @"age":@"age_1",
                 @"info":@[@"info_1",@"info_2",@"info_3"]
                 };
    }
    

    处理集合类型

    //在对象中存在数组等集合类型情况处理
      NSDictionary *dict = @{
                               @"name_1":@"testName",
                               @"age_1":@(19),
                               @"info_1":@"testInfo~~~",
                               @"stu":@{@"name":@"gee",
                                        @"number":@"123456"},
                               @"list":@[
                                         @{
                                           @"name":@"test1",
                                           @"price":@"100",
                                          },
                                         @{
                                           @"name":@"test1",
                                           @"price":@"100",
                                          },
                                         ]
                               };
        Person *p = [Person modelWithDictionary:dict];
        NSLog(@"%@-%d-%@-%@-%@-%@",p.name,p.age,p.info,p.stu.name,p.stu.number,p.list);
        for (Goods *dict in p.list) {
            NSLog(@"%@",dict.name);
        }
        NSDictionary *dict_ = [p modelToJSONObject];
        NSLog(@"%@",dict_);
    
    //声明list为Goods对象
    +(NSDictionary *)modelContainerPropertyGenericClass{
        return @{
                 @"list":Goods.class
                 };
    }
    

    也可以单独处理(将一个数组对象转成模型),核心代码modelArrayWithClass,参数1就是转换后存的模型,参数2就是数据。

     NSArray * array = @[
                            @{
                                @"name":@"test1",
                                @"price":@"100",
                                },
                            @{
                                @"name":@"test1",
                                @"price":@"100",
                                },
                            ];
        NSArray *goods  = [NSArray modelArrayWithClass:[Goods class] json:array];
        NSLog(@"%@",goods);
    }
    

    YYModel归档

    //解档
    -(instancetype)initWithCoder:(NSCoder *)aDecoder
    {
        self = [super init];
        //不用yymodel写法 如果有很多的属性,代码就显得很多了
        //    if (self) {
        //       self.name = [aDecoder decodeObjectForKey:@"name"];
        //    }
        return  [self modelInitWithCoder:aDecoder];
    }
    //归档
    -(void)encodeWithCoder:(NSCoder *)aCoder{
        //不用yymodel写法 如果有很多的属性,代码就显得很多了
        // [aCoder encodeObject:self.name forKey:@"name"];
        [self modelEncodeWithCoder:aCoder];
       
    }
    
    

    相关文章

      网友评论

          本文标题:YYmodel-数据转模型

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