//
// main.m
// oc06 --
省市区字典
//
// Created by dllo on 15/7/22.
// Copyright (c) 2015年 Gaozi. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc,
const char * argv[]) {
//准备工作
NSString *path =
@"/Users/dllo/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"];
// 再把temp放入
[zonearr
addObject:temp];
}
}
// fotin
遍历.
for (NSMutableDictionary *prodic
in proArr) {
NSLog(@"%@",prodic[@"proName"]);
NSMutableArray *cityArr = prodic[@"cityArr"];
for (NSMutableDictionary *citydic
in cityArr) {
NSLog(@"%@",citydic[@"cityName"]);
NSMutableArray *zonearr = citydic[@"zoneArr"];
for (NSString *zoneName
in zonearr) {
NSLog(@"%@",zoneName);
}
}
NSLog(@"\n");
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。