用模型取代字典

使用字典的坏处

一般情况下,设置数据和取出数据都使用“字符串类型的key”,编写这些key时,编辑器没有智能提示,需要手敲
dict[@"name"] = @"Jack";
NSString *name = dict[@"name"];
  • 手敲字符串key,key容易写错
  • Key如果写错了,编译器不会有任何警告和报错,造成设错数据或者取错数据
使用模型的好处
  • 所谓模型,其实就是数据模型,专门用来存放数据的对象,用它来表示数据会更加专业
  • 模型设置数据和取出数据都是通过它的属性,属性名如果写错了,编译器会马上报错,因此,保证了数据的正确性
  • 使用模型访问属性时,编译器会提供一系列的提示,提高编码效率
app.name = @"Jack";
NSString *name = app.name;

字典转模型

1.字典转模型的过程最好封装在模型内部
2.模型应该提供一个可以传入字典参数的构造方法
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)xxxWithDict:(NSDictionary *)dict;

instancetype与id区别

id:代指所有类型,但是使用不恰当的类型指针指向不匹配的对象时,编译器不会报错、警告,但在运行时很可能会出错

instantcetype:
  1. instancetype在类型表示上,跟id一样,可以表示任何对象类型
  2. instancetype只能用在返回值类型上,不能像id一样用在参数类型上
  3. instancetype比id多一个好处:编译器会检测instancetype的真实类型

编码实现

UYAppInfo.h

//
// UYAppInfo.h
// 4.0应用程序管理
#import <UIKit/UIKit.h> @interface UYAppInfo : UITabBar
@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *icon; /** 通常在写模型的实例化方法时,以下两个方法,都需要实现 */ /** 使用字典实例化模型 */
- (instancetype)initWithDict:(NSDictionary *)dict;
/** 类方法可以快速实例化一个对象 */
+ (instancetype)appInfoWithDict:(NSDictionary *)dict; /** 返回所有plist中的数据模型数组 */
+ (NSArray *)appList;
@end

UYAppInfo.m

//
// UYAppInfo.m
// 4.0应用程序管理
//
// Created by jiangys on 15/8/23.
// Copyright (c) 2015年 uxiaoyuan. All rights reserved.
// #import "UYAppInfo.h" @implementation UYAppInfo - (instancetype)initWithDict:(NSDictionary *)dict
{
// self 是 对象
self = [super init];
if (self) {
// 方法一:
// 用字典给属性赋值,所有与plist键值有关的方法,均在此处!
// self.name = dict[@"name"];
// self.icon = dict[@"icon"]; // 方法二:
// KVC - key value coding键值编码
// 是一种间接修改/读取对象属性的一种方法
// KVC 被称为 cocoa 的大招!
// 参数:
// 1. 数值
// 2. 属性名称
// [self setValue:dict[@"name"] forKeyPath:@"name"];
// [self setValue:dict[@"icon"] forKeyPath:@"icon"];
// setValuesForKeysWithDictionary本质上就是调用以上两句代码
[self setValuesForKeysWithDictionary:dict];
}
return self;
} + (instancetype)appInfoWithDict:(NSDictionary *)dict
{
// self 是 class
return [[self alloc] initWithDict:dict];
} + (NSArray *)appList
{
NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
// 创建一个临时数组
NSMutableArray *arrayM = [NSMutableArray array];
// 遍历数组,依次转换模型
for (NSDictionary *dict in array) {
[arrayM addObject:[UYAppInfo appInfoWithDict:dict]];
} return arrayM;
} @end

实际上,这部分的格式更像:

// 对象方法
- (instancetype) initWith数据源名:(NSDictionary *) 数据源 {
if (self = [super init]) {
self.属性 = 数据源[KEY名];
}
return self;
} // 类方法
+ (instancetype) 类名With数据源名:(NSDictionary *) 数据源 {
return [[self alloc] initWith数据源名:数据源];
}

最后的实现就很简单了

UYViewController.m

-(NSArray *) appList
{
if (nil==_appList) {
//NSString *path=[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
//_appList=[NSArray arrayWithContentsOfFile:path];
_appList=[UYAppInfo appList];
}
return _appList;
}

获取一个对象:

UYAppInfo *appData=self.appList[i];

使用model:

lable.text = appData.name;

最新文章

  1. 【blade利刃出鞘】一起进入移动端webapp开发吧
  2. java使用代理 html2canvas 截屏 将页面内容生成图片
  3. hdu 4982 Goffi and Squary Partition
  4. pip高级使用技巧以及搭建自己的pypi服务器
  5. hdu 5713(状态压缩DP)
  6. Domino 迁移到Exchange 服务器 之在Domino Server 创建用户!
  7. android 混淆配置
  8. 树莓派的演奏音符3 -- LCD1602显示文章
  9. Web API之消息处理管道
  10. php类与构造函数解析
  11. Java基础知识二次学习--第六章 常用类
  12. svn: Can&#39;t convert string from &#39;UTF-8&#39; to native
  13. 【BZOJ3669】【Noi2014】魔法森林(Link-Cut Tree)
  14. Android快速关联V4包的方式
  15. 将选中的物体写入XML文件
  16. BERT(Bidirectional Encoder Representations from Transformers)
  17. Play 2D games on Pixel running Android Nougat (N7.1.2) with Daydream View VR headset
  18. nowcoder提高集训营第5场
  19. mass create DN
  20. 基于cookie的SSO单点登录系统

热门文章

  1. Java 反编译工具 —— JAD 的下载地址(Windows版/Linux版/Mac OS 版)
  2. imsdroid 学习(初认识)
  3. Android 逆向工具
  4. Android.mk (1) 函数
  5. Linux命令 dmesg:分析内核产生的信息
  6. Elasticsearch 学习之 分片未分配原因
  7. SVN服务端安装
  8. Nhibernate学习
  9. 开发工具-网络封包分析工具Charles
  10. 什么是webpack?