美文网首页
MJExtension模型字典互转

MJExtension模型字典互转

作者: 哈哈大p孩 | 来源:发表于2016-07-05 10:15 被阅读7032次

    MJExtension是大神李明杰写的模型字典互转的一个第三方,只用一段代码就能实现简单以及复杂的转换问题,非常方便,今天学习了一下,下面写几个常用的。在这里还要感谢一下@PP_Abner帮忙解决问题,请大家下载最新第三方(老版本会有问题,本人亲测)。
    废话少说,进入主题。
    将MJExtension导入工程中,然后无需再做任何操作,他会自动让你新建的model继承他里面的方法(真是天然无公害呀。。)

    1.字典转模型
    MyModel.h

    @property (copy,nonatomic) NSString *name;
    @property (nonatomic, strong) NSString *sex;
    @property (nonatomic, assign) int age;
    @property (nonatomic, assign) float money;
    

    viewController.m

    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"男",@"sex",@"20",@"age",@"1000",@"money", nil];
        MyModel *model =  [MyModel mj_objectWithKeyValues:dic]; //在这里一句话就转了过去
        NSLog(@"name == %@, age == %d, sex == %@, money == %.2f", model.name,model.age,model.sex,model.money);
    

    2.模型转模型
    InfoModel.h

    @property (nonatomic, strong) MyModel *JJmymodel;
    @property (nonatomic, strong) NSString *schName;
    @property (nonatomic, assign) int StuNumber;
    

    viewController.m

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"北大",@"schName",@"20000",@"StuNumber",dic,@"JJmymodel", nil]; //在这个字典中,有个数据是mymodel类型的
        InfoModel *inModel = [InfoModel mj_objectWithKeyValues:dict];
        NSLog(@"schName == %@, stuNum == %d, name == %@, age == %d, sex == %@, money == %.2f ", inModel.schName, inModel.StuNumber, inModel.JJmymodel.name, inModel.JJmymodel.age, inModel.JJmymodel.sex, inModel.JJmymodel.money);
    

    3.模型中的属性名和字典中的key不相同(或需要多级映射)
    AnimalModel.h

    @property (nonatomic, strong) NSString *Mydog;
    @property (nonatomic, strong) NSString *Mypig;
    @property (nonatomic , strong) NSString *Smallbird;
    @property (nonatomic, strong) NSString *Bluecat;
    

    数据源中的参数和我们model中定义的参数名不同或者是多级映射关系的,则需要我们在model的.m文件中写个+号方法,他会自动实现

    + (NSDictionary *)mj_replacedKeyFromPropertyName {
        return @{
                 @"Mydog":@"dog",
                 @"Mypig":@"pig",
                 @"Smallbird":@"bird.twoBird",
                 @"Bluecat":@"mycat.bigCat"
                 };  //从这里可以看出,model中的Mydog对应我们数据源中的dog,Smallbird对应bird.twoBird
    }
    

    viewController.m

     NSDictionary *AnimatDic = @{@"dog":@"小狗",
                                    @"pig":@"我的小猪",
                                    @"bird":@{@"oneBird":@"111",
                                              @"twoBird":@"222",
                                              @"threeBird":@"333"},
                                    @"mycat":@{@"smallCat":@"kity",
                                               @"bigCat":@"tager"}}; //数据源
        AnimalModel *animalModel =  [AnimalModel mj_objectWithKeyValues:AnimatDic];
        NSLog(@"dog == %@, pig == %@, bird == %@, cat == %@", animalModel.Mydog, animalModel.Mypig, animalModel.Smallbird, animalModel.Bluecat);
    

    其他的以后有机会再补充完整,暂时常用的就这几个。

    相关文章

      网友评论

          本文标题:MJExtension模型字典互转

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