标签:
标签:
1.下面这样的数组,怎么转成模型

2.分析

3.新增模型:城市分组,城市,区,每个模型都有name,抽一个父类

4.每个模型分别具备的属性
BaseModel : NSObject 属性 NSString *name
CitySection : BaseModel 属性 NSArray *cities
City : BaseModel 属性 NSArray *districts; 属性 BOOL hot;
District : BaseModel 属性 NSArray *neighborhoods
5.控制器
5.1 导入框架 #import "MJExtension.h"
5.2 控制器的成员属性 : @property (nonatomic,strong) NSArray *citySections;
5.3 延迟加载
1 - (NSArray *)citySections
2 {
3 if (_citySections == nil) {
4 NSString *path = [[NSBundle mainBundle] pathForResource:@"Cities.plist" ofType:nil];
5 NSArray *dataArray = [NSArray arrayWithContentsOfFile:path];
6 NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:dataArray.count];
7
8 for (NSDictionary *dict in dataArray) {
9 // MJExtension框架里,字典转模型的方法
10 CitySection *section = [CitySection objectWithKeyValues:dict];
11 [arrayM addObject:section];
12 }
13 _citySections = arrayM;
14 }
15
16 return _citySections;
17 }
6 在模型类的.m文件中
6.1导入框架 #import "MJExtension.h"
6.2 实现方法
目的是告诉控制器的 objectWithKeyValues: 方法 , cities数组里面装的时city模型
// 在CitySection.m中
- (NSDictionary *)objectClassInArray
{
return @{ @"cities" : [City class] };
}
// 在 City.m中
- (NSDictionary *)objectClassInArray
{
return @{ @"districts" : [District class]};
}
注:District.m中,就不用这么干了,因为 District模型的属性 NSArray *neighborhoods 里装的是NSString
7 看看转换的结果

标签:
原文地址:http://www.cnblogs.com/wangbinios/p/5008360.html