黑马程序员--字典转模型
2015-06-23 21:05
621 查看
字典转模型
if (_appPlist == nil) {
// _appPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
NSArray *array =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
NSMutableArray *arrayM = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
// appInfo *appinfo = [appInfo appInfoWithDict:dict];
// appInfo *appinfo = [[appInfo alloc] initWithDict:dict];
// appinfo.name = dict[@"name"];
// appinfo.icon = dict[@"icon"];
[arrayM addObject:[appInfo appInfoWithDict:dict]];
}
_appPlist = arrayM;
}
return _appPlist;
转模型过程中,appInfo 自定义 initWithDict方法
/**
使用KVC的注意事项
1> plist中的键值名称必须与模型中的属性一致
2> 模型中的属性可以不全部出现在plist中
*/
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
// _name = dict[@"name"];
// _icon = dict[@"icon"];
// [self setValue:dict[@"name"] forKeyPath:@"name"];
// [self setValue:dict[@"icon"] forKeyPath:@"icon"];
[self setValuesForKeysWithDictionary:dict]; //KVC
}
return self;
}
+(instancetype)appInfoWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
if (_appPlist == nil) {
// _appPlist = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
NSArray *array =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil]];
NSMutableArray *arrayM = [[NSMutableArray alloc] init];
for (NSDictionary *dict in array) {
// appInfo *appinfo = [appInfo appInfoWithDict:dict];
// appInfo *appinfo = [[appInfo alloc] initWithDict:dict];
// appinfo.name = dict[@"name"];
// appinfo.icon = dict[@"icon"];
[arrayM addObject:[appInfo appInfoWithDict:dict]];
}
_appPlist = arrayM;
}
return _appPlist;
转模型过程中,appInfo 自定义 initWithDict方法
/**
使用KVC的注意事项
1> plist中的键值名称必须与模型中的属性一致
2> 模型中的属性可以不全部出现在plist中
*/
- (instancetype)initWithDict:(NSDictionary *)dict
{
self = [super init];
if (self) {
// _name = dict[@"name"];
// _icon = dict[@"icon"];
// [self setValue:dict[@"name"] forKeyPath:@"name"];
// [self setValue:dict[@"icon"] forKeyPath:@"icon"];
[self setValuesForKeysWithDictionary:dict]; //KVC
}
return self;
}
+(instancetype)appInfoWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
相关文章推荐
- 黑马程序员--搭建界面,九宫格
- 黑马程序员--完整的设计一个动画从开始到退出
- 面试题25 二叉树中和为某一值的路径
- while MyJob = '程序员' do --- 序
- 黑马程序员---------集合
- 剑指offer--面试题26:复杂链表的复制--Java实现
- 面试常见算法-排序查找算法
- 黑马程序员_关于本人对面向对象的一点小看法
- 黑马程序员--刚学完集合,也看了一些资料,做个总结,
- 几个面试经典算法题Java解答
- 黑马程序员_JAVA的异常处理_2_总结
- 黑马程序员——java高新技术——反射
- LR经典面试题
- 黑马程序员--OC--分类和重新定义description方法
- 黑马程序员--set和get方法
- 面试总结2
- 黑马程序员--面向对象的语法
- 为Java程序员编写的Scala的入门教程
- 黑马程序员——Java基础:多线程
- 黑马程序员--Java学习日记之总结复习(多线程和设计模式)