- 首页 > it技术 > >
//准备工作
NSString *path = @"/Users/dlios/Desktop/OC/OC06_省市区字典数组/OC06_省市区字典数组/area.txt";
//根据路径产生响应的字符串
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//对字符串进行切割
NSArray *strArr =[str componentsSeparatedByString:@"\n"];
//创建一个省数组
NSMutableArray *proArr = [NSMutableArray array];
for (NSString *temp in strArr) {
if (![temp hasPrefix: @" "]) {
//创建一个省的字典
NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
//给省字典添加键值对
//省名
[proDic setObject:temp forKey:@"proName"];
//创建一个市数组
NSMutableArray *cityArr = [NSMutableArray array];
//添加到省字典中
[proDic setObject:cityArr forKey:@"cityArr"];
//把省字典放入到省数组中
[proArr addObject:proDic];
}else if ([temp hasPrefix:@""] && ![temp hasPrefix:@""]){
//找到对应城市
//创建市字典
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
//第一对 市名
[cityDic setObject:temp forKey:@"cityName"];
//第二对 区数组
NSMutableArray *zoneArr = [NSMutableArray array];
[cityDic setObject:zoneArr forKey:@"zoneArr"];
//给市字典找到一个位置
//先找到对应的省字典
NSMutableDictionary *proDic = [proArr lastObject];
//再找对应的市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
//再把市字典放到市数组中
[cityArr addObject:cityDic];
}else{
//先找省字典
NSMutableDictionary *proDic = [proArr lastObject];
//找市数组
NSMutableArray *cityArr = proDic[@"cityArr"];
//市字典
NSMutableDictionary *cityDic = [cityArr lastObject];
//区数组
NSMutableArray *zoneArr = cityDic[@"zoneArr"];
//添加
[zoneArr addObject:temp];
}
} //fonin遍历省数组
for (NSMutableDictionary *dic in proArr) {
NSLog(@"%@", dic[@"proName"]);
NSMutableArray *cityArr = dic[@"cityArr"];
for (NSMutableDictionary *cityDic in cityArr) {
NSLog(@"%@", cityDic[@"cityName"]);
NSMutableArray *zoneArr = cityDic[@"zoneArr"];
for (NSString *str in zoneArr) {
NSLog(@"%@", str);
}
}}
推荐阅读