您的位置:首页 > 其它

字典转模型简单介绍

2016-06-01 16:47 393 查看

字典转模型简单介绍

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

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

//  CarBrand.h
//  字典转模型
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 KXZDJ. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Cars.h"

@interface CarBrand : NSObject
/**
*  cars数组
*/
@property (nonatomic, strong) NSArray *cars;
/**
*  title
*/
@property (nonatomic, copy) NSString *title;

/**
*  字典转模型的对象方法
*
*  @param dict 第一层数组中的字典
*
*  @return
*/
-(instancetype)initWithDict:(NSDictionary *)dict;

/**
*  字典转模型的类方法
*/
+(instancetype)carBrandWithDict:(NSDictionary *)dict;

/**
*  定义一个类方法,来加载plist文件,获取Root数组,返回这个数组
*/
+(NSMutableArray *)carBrand;

@end


CarBrand.m

//
//  CarBrand.m
//  字典转模型
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 KXZDJ. All rights reserved.
//

#import "CarBrand.h"

@implementation CarBrand
/**
*  实现字典转模型的对象方法
*/
-(instancetype)initWithDict:(NSDictionary *)dict {

if (self = [super init]) {
//给属性赋值
self.cars = dict[@"cars"];
self.title = dict[@"title"];
}
return self;
}
/**
*  实现字典转模型的类方法
*/
+(instancetype)carBrandWithDict:(NSDictionary *)dict {
return [[self alloc] initWithDict:dict];
}

+(NSMutableArray *)carBrand {
//   1.加载plist
//   获取绝对路径
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"cars_total" ofType:@"plist"];
//   读取数组(分组的字典)
NSArray *array = [NSArray arrayWithContentsOfFile:filePath];

//  2.把array中分组的字典转为模型

//  定义一个可变数组接收转换完成的所有模型
NSMutableArray *arrayM = [NSMutableArray array];

//  遍历array,把它里面存放的组字典转换为组模型,放到arrayM中
for (NSDictionary *dict in array) {
CarBrand *carBand  = [CarBrand carBrandWithDict:dict];

//      这个字典数组
NSArray *dictArray  = carBand.cars;

//      把dictArray转换为一个Cars的模型数组
//      定义一个可变数组用来存放转换后的Cars模型
NSMutableArray *carsArray = [NSMutableArray array];

//      遍历字典数组dictArray,把字典转换为Cars的模型
for (NSDictionary *dict in dictArray) {

//        把字典转换为Cars的模型
Cars *car  = [Cars carWithDict:dict];

//         把转换后的car添加到carsArray数组中
[carsArray addObject:car];
}
//     把转换后的CZCar的模型数组赋值给carGroup的cars属性
carBand.cars = carsArray;

[arrayM addObject:carBand];
}
// 3.返回组模型数组
return arrayM;
}

@end


Cars.h

//
//  Cars.h
//  字典转模型
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 KXZDJ. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Cars : NSObject
/**
*  汽车名称
*/
@property (nonatomic,copy) NSString *name;

/**
*  汽车的图标
*/
@property (nonatomic,copy) NSString *icon;
/**
*  汽车信息
*/
@property (nonatomic, copy) NSString *info;
/**
*  产地
*/
@property (nonatomic, copy) NSString *country;
//实现字典转模型的方法
// 对象方法
- (instancetype) initWithDict:(NSDictionary *) dict;

//类方法
+ (instancetype) carWithDict:(NSDictionary *) dict;
@end


Cars.m

//
//  Cars.m
//  字典转模型
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 KXZDJ. All rights reserved.
//

#import "Cars.h"

@implementation Cars
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {

self.icon = dict[@"icon"];
self.name = dict[@"name"];
self.country = dict[@"country"];
self.info = dict[@"info"];

}
return self;
}

+ (instancetype)carWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
@end


ViewController.m

//
//  ViewController.m
//  字典转模型
//
//  Created by admin on 16/6/1.
//  Copyright © 2016年 KXZDJ. All rights reserved.
//

#import "ViewController.h"
#import "CarBrand.h"
#import "Cars.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
//定义一个接收模型的属性
@property (nonatomic,strong) NSArray *carBrand;

//定义一个tableView现实模型数据
@property (nonatomic, strong) UITableView *myTableView;

@end

@implementation ViewController
//1.重写getter方法
//2.判断如果没有数据,才去加载
-(NSArray *)carBrand {
if (_carBrand == nil) {
//如果_carBrand数组为空,就创建数组并调用CarBrand的类方法;
_carBrand = [CarBrand carBrand];
}
//3.返回加载后的数据
return _carBrand;
}

- (void)viewDidLoad {
[super viewDidLoad];
[self configure];
}

-(void)configure{
//创建myTableView
self.myTableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
//遵守代理和数据源
self.myTableView.delegate = self;
self.myTableView.dataSource = self;
//添加到self.view上
[self.view addSubview:self.myTableView];
}

#pragma mark - 数据源方法
//一共有多少组
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return self.carBrand.count;
}
// 每一组显示什么样内容
// 告诉tableView第section组有多少行数据
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//  取出一个模型对象
CarBrand *carBrand   = self.carBrand[section];
//  返回分组中汽车数量
return carBrand.cars.count;
}
// 每一行显示什么内容
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier=@"car";
//取出KXZCarGroup数据模型
CarBrand *carBrand = self.carBrand[indexPath.section];
//取出KXZCar模型
Cars *car = carBrand.cars[indexPath.row];
//去缓存中去取cell
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
//若没有,则创建cell,并盖章
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
//给cell赋值
cell.textLabel.text = car.name;
cell.detailTextLabel.text = car.country;
//返回cell
return cell;
}
@end


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

运行效果图:


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

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