字典转模型简单介绍

字典转模型简单介绍

一.为什么要字典转模型?

1.使用字典:

  1.1使用字典的时候,都是通过Key来取值,Key一般都是字符串,字符串就容易写错

  1.2key写错了,编译不会报错,很难找出错误

  1.3没有提示,降低编码速度

2.使用模型

模型是一种专门用来存储数据的对象

  2.1有代码提示

  2.2如果键写错,编译就会报错,方便找出错误并修改

  2.3相对专业

二.字典转模型的步骤?

1.定义模型类

2.声明模型属性

  2.1字典中一个key对应一个属性

  2.2并保持key与属性名字最好相同

  2.3字典中key对应的值是什么类型,那么模型中对应的属性就是什么类型.

3.实现对象转模型的对象方法和类方法

 

三.准备工作

1.拖入创建好的plist文件,并分析plist文件的结构

上图所示的plist文件中可以看出:

最外面是一个Root数组,这个数组中包含了0到4这5个字典,每个字典中,又有一个cars数组和一个string,cars数组中又包含了一个字典,字典中4个string.

不难看出,这个结构如下图:

根据上面的分析,咋们创建两个模型分别来接收这两个字典转成的模型

创建模型:

 

 

创建好模型后,在各自模型中定义各自的属性:

CarBrand.h

 1 //  CarBrand.h
 2 //  字典转模型
 3 //
 4 //  Created by admin on 16/6/1.
 5 //  Copyright © 2016年 KXZDJ. All rights reserved.
 6 //
 7 
 8 #import <Foundation/Foundation.h>
 9 #import "Cars.h"
10 
11 @interface CarBrand : NSObject
12 /**
13  *  cars数组
14  */
15 @property (nonatomic, strong) NSArray *cars;
16 /**
17  *  title
18  */
19 @property (nonatomic, copy) NSString *title;
20 
21 /**
22  *  字典转模型的对象方法
23  *
24  *  @param dict 第一层数组中的字典
25  *
26  *  @return
27  */
28 -(instancetype)initWithDict:(NSDictionary *)dict;
29 
30 /**
31  *  字典转模型的类方法
32  */
33 +(instancetype)carBrandWithDict:(NSDictionary *)dict;
34 
35 
36 /**
37  *  定义一个类方法,来加载plist文件,获取Root数组,返回这个数组
38  */
39 +(NSMutableArray *)carBrand;
40 
41 @end

 

CarBrand.m

 1 //
 2 //  CarBrand.m
 3 //  字典转模型
 4 //
 5 //  Created by admin on 16/6/1.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "CarBrand.h"
10 
11 @implementation CarBrand
12 /**
13  *  实现字典转模型的对象方法
14  */
15 -(instancetype)initWithDict:(NSDictionary *)dict {
16     
17     if (self = [super init]) {
18         //给属性赋值
19         self.cars = dict[@"cars"];
20         self.title = dict[@"title"];
21     }
22     return self;
23 }
24 /**
25  *  实现字典转模型的类方法
26  */
27 +(instancetype)carBrandWithDict:(NSDictionary *)dict {
28     return [[self alloc] initWithDict:dict];
29 }
30 
31 
32 +(NSMutableArray *)carBrand {
33     //   1.加载plist
34     //   获取绝对路径
35     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
36     //   读取数组(分组的字典)
37     NSArray *array = [NSArray arrayWithContentsOfFile:filePath];
38     
39     //  2.把array中分组的字典转为模型
40     
41     //  定义一个可变数组接收转换完成的所有模型
42     NSMutableArray *arrayM = [NSMutableArray array];
43     
44     //  遍历array,把它里面存放的组字典转换为组模型,放到arrayM中
45     for (NSDictionary *dict in array) {
46         CarBrand *carBand  = [CarBrand carBrandWithDict:dict];
47         
48         //      这个字典数组
49         NSArray *dictArray  = carBand.cars;
50         
51         //      把dictArray转换为一个Cars的模型数组
52         //      定义一个可变数组用来存放转换后的Cars模型
53         NSMutableArray *carsArray = [NSMutableArray array];
54         
55         //      遍历字典数组dictArray,把字典转换为Cars的模型
56         for (NSDictionary *dict in dictArray) {
57             
58             //        把字典转换为Cars的模型
59             Cars *car  = [Cars carWithDict:dict];
60             
61             //         把转换后的car添加到carsArray数组中
62             [carsArray addObject:car];
63         }
64         //     把转换后的CZCar的模型数组赋值给carGroup的cars属性
65         carBand.cars = carsArray;
66         
67         [arrayM addObject:carBand];
68     }
69     // 3.返回组模型数组
70     return arrayM;
71 }
72 
73 
74 
75 @end

Cars.h

 1 //
 2 //  Cars.h
 3 //  字典转模型
 4 //
 5 //  Created by admin on 16/6/1.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface Cars : NSObject
12 /**
13  *  汽车名称
14  */
15 @property (nonatomic,copy) NSString *name;
16 
17 /**
18  *  汽车的图标
19  */
20 @property (nonatomic,copy) NSString *icon;
21 /**
22  *  汽车信息
23  */
24 @property (nonatomic, copy) NSString *info;
25 /**
26  *  产地
27  */
28 @property (nonatomic, copy) NSString *country;
29 //实现字典转模型的方法
30 // 对象方法
31 - (instancetype) initWithDict:(NSDictionary *) dict;
32 
33 //类方法
34 + (instancetype) carWithDict:(NSDictionary *) dict;
35 @end

Cars.m

 1 //
 2 //  Cars.m
 3 //  字典转模型
 4 //
 5 //  Created by admin on 16/6/1.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "Cars.h"
10 
11 @implementation Cars
12 - (instancetype)initWithDict:(NSDictionary *)dict
13 {
14     if (self = [super init]) {
15         
16         self.icon = dict[@"icon"];
17         self.name = dict[@"name"];
18         self.country = dict[@"country"];
19         self.info = dict[@"info"];
20         
21     }
22     return self;
23 }
24 
25 + (instancetype)carWithDict:(NSDictionary *)dict
26 {
27     return [[self alloc] initWithDict:dict];
28 }
29 @end

ViewController.m

 1 //
 2 //  ViewController.m
 3 //  字典转模型
 4 //
 5 //  Created by admin on 16/6/1.
 6 //  Copyright © 2016年 KXZDJ. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "CarBrand.h"
11 #import "Cars.h"
12 
13 @interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
14 //定义一个接收模型的属性
15 @property (nonatomic,strong) NSArray *carBrand;
16 
17 //定义一个tableView现实模型数据
18 @property (nonatomic, strong) UITableView *myTableView;
19 
20 @end
21 
22 @implementation ViewController
23 //1.重写getter方法
24 //2.判断如果没有数据,才去加载
25 -(NSArray *)carBrand {
26     if (_carBrand == nil) {
27         //如果_carBrand数组为空,就创建数组并调用CarBrand的类方法;
28         _carBrand = [CarBrand carBrand];
29     }
30     //3.返回加载后的数据
31     return _carBrand;
32 }
33 
34 - (void)viewDidLoad {
35     [super viewDidLoad];
36     [self configure];
37 }
38 
39 
40 -(void)configure{
41     //创建myTableView
42     self.myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
43     //遵守代理和数据源
44     self.myTableView.delegate = self;
45     self.myTableView.dataSource = self;
46     //添加到self.view上
47     [self.view addSubview:self.myTableView];
48 }
49 
50 #pragma mark - 数据源方法
51 //一共有多少组
52 - (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
53 {
54     return self.carBrand.count;
55 }
56 // 每一组显示什么样内容
57 // 告诉tableView第section组有多少行数据
58 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
59 {
60     //  取出一个模型对象
61     CarBrand *carBrand   = self.carBrand[section];
62     //  返回分组中汽车数量
63     return carBrand.cars.count;
64 }
65 // 每一行显示什么内容
66 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
67 {
68     static NSString *identifier=@"car";
69     //取出KXZCarGroup数据模型
70     CarBrand *carBrand = self.carBrand[indexPath.section];
71     //取出KXZCar模型
72     Cars *car = carBrand.cars[indexPath.row];
73     //去缓存中去取cell
74     UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
75     //若没有,则创建cell,并盖章
76     if (cell==nil) {
77         cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
78     }
79     //给cell赋值
80     cell.textLabel.text = car.name;
81     cell.detailTextLabel.text = car.country;
82     //返回cell
83     return cell;
84 }
85 @end

这里用的tableView来展示数据,而且用的系统自带的UITableViewCell,所以只现实了name和country

 运行效果图:

以上是对字典转模型的一个简单介绍,如有不足,敬请指正.

posted on 2016-06-01 16:47  Xib'sStory  阅读(445)  评论(0编辑  收藏  举报

导航