ViewController.m
#import "ViewController.h"
#import "Province.h"
#import "City.h"
#import "Area.h"
@interface ViewController ()
//存对象的array
@property (nonatomic,retain)NSMutableArray *provinceArray;
@end
@implementation ViewController
-(void)getData{
self.provinceArray = [NSMutableArray array];
//存字典的array
NSMutableArray *provinceArray = [NSMutableArray array];
NSString *contentString = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Desktop/实训/UI08-省市区 -模型嵌套/UI08-省市区 -模型嵌套/area.txt" encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@",contentString);
NSArray *lineArray = [contentString componentsSeparatedByString:@"\n"];
for (NSString *lineString in lineArray) {
if (![lineString hasPrefix:@" "]) {
NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
[provinceDic setObject:lineString forKey:@"provinceName"];
NSMutableArray *cityArray = [NSMutableArray array];
[provinceDic setObject:cityArray forKey:@"cityArray"];
[provinceArray addObject:provinceDic];
}else if (![lineString hasPrefix:@" "]){
NSMutableDictionary *provinceDic = [provinceArray lastObject];
NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
[cityDic setObject:lineString forKey:@"cityName"];
NSMutableArray *areaArray = [NSMutableArray array];
[cityDic setObject:areaArray forKey:@"areaArray"];
[cityArray addObject:cityDic];
}else{
NSMutableDictionary *provinceDic = [provinceArray lastObject];
NSMutableArray *cityArray = [provinceDic objectForKey:@"cityArray"];
NSMutableDictionary *cityDic = [cityArray lastObject];
NSMutableArray *areaArray = [cityDic objectForKey:@"areaArray"];
[areaArray addObject:lineString];
}
}
for (NSDictionary *provinceDic in provinceArray) {
Province *province = [[Province alloc] init];
[province setValuesForKeysWithDictionary:provinceDic];
[self.provinceArray addObject:province];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self getData];
for (Province *province in self.provinceArray) {
NSLog(@"province name:%@",province.provinceName);
for (City *city in province.cityArray) {
NSLog(@"city name:%@",city.cityName);
for (Area *area in city.areaArray) {
NSLog(@"area name:%@",area.areaName);
}
}
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
BaseModel.m 这是基类
-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
}
Province.h这是一个城市的对象 继承于基类
@property (nonatomic,retain)NSString *provinceName;
@property (nonatomic,retain)NSArray *cityArray;
Province.m
#import "Province.h"
#import "City.h"
@implementation Province
-(void)setValue:(id)value forKey:(NSString *)key{
if ([key isEqualToString:@"cityArray"]) {
NSMutableArray *cityArray = [[NSMutableArray alloc] init];
for (NSDictionary *cityDic in value) {
City *city = [[City alloc] init];
[city setValuesForKeysWithDictionary:cityDic];
[cityArray addObject:city];
}
self.cityArray = [cityArray copy];
}else{
[super setValue:value forKey:key];
}
}
@end
City.h
#import "BaseModel.h"
@interface City : BaseModel
@property (nonatomic,retain)NSString *cityName;
@property (nonatomic,retain)NSArray *areaArray;
@end
City.m
#import "City.h"
#import "Area.h"
@implementation City
-(void)setValue:(id)value forKey:(NSString *)key{
if ([key isEqualToString:@"areaArray"]) {
NSMutableArray *areaArray = [NSMutableArray array];
for (NSString *areaName in value) {
Area *area = [[Area alloc] init];
//[area setAreaName:areaName];
area.areaName = areaName;
[areaArray addObject:area];
}
self.areaArray = [areaArray copy];
} else {
[super setValue:value forKey:key];
}
}
Area.h
@property (nonatomic,retain)NSString *areaName;
这是根据setValue来解析的,大家可以看看我之前发的思维图来找找思路哦
网友评论