一 OC小地图之通过省市来输出区域
<span style="font-size:14px;"> NSString *filePath = @"/Users/dlios/Desktop/666666/20150302demo01/20150302demo01/area.txt";</span>
//把文件装入缓冲区
<span style="font-size:14px;"> NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
<span style="color:#009900;">//把字符串拆分成数组</span>
NSArray *array = [buffer componentsSeparatedByString:@"\n"];</span>
<span style="font-size:14px;"> </span><span style="font-size:12px;color:#009900;">//创建省份数组</span><span style="font-size:14px;">
NSMutableArray *provinceList = [NSMutableArray array];
NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" 0123456789"];
for (NSString *s in array) {</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span><span style="color:#009900;">//找到省份</span></span>
<span style="font-size:14px;"> if (![s hasPrefix:@" "]) {</span>
<span style="font-size:14px;"><span style="color:#009900;"> //省名称(去掉两边的空格和数字)</span>
NSString *provinceName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建城市数组</span>
NSMutableArray *cityList = [NSMutableArray array];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建省字典</span>
NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:provinceName, @"name", cityList, @"list", nil];</span>
<span style="font-size:14px;"> <span style="color:#009900;background-color: rgb(255, 255, 255);">//装入省字典</span>
[provinceList addObject:pDic];
}</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到城市</span>
if ([s hasPrefix:@" "] && ![s hasPrefix:@" "]) {</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到省字典</span>
NSDictionary *pDic = [provinceList lastObject];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到城市数组</span>
NSMutableArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;"> /<span style="color:#009900;">/城市名称(去掉两边的空格和数字)</span>
NSString *cityName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建区数组</span>
NSMutableArray *districtList = [NSMutableArray array];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建城市字典</span>
NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:cityName, @"name", districtList, @"list", nil]; </span>
<span style="font-size:14px;"><span style="white-space:pre"> </span> <span style="color:#009900;">//装入城市字典</span>
[cityList addObject:cDic];
}</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到区</span>
if ([s hasPrefix:@" "]) {</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到省字典</span>
NSDictionary *pDic = [provinceList lastObject];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到城市数组</span>
NSArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到城市字典</span>
NSDictionary *cDic = [cityList lastObject];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//找到区数组</span>
NSMutableArray *districtList = [cDic objectForKey:@"list"];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//区名称(去掉两边的空格和数字)</span>
NSString *dName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//装入区数组</span>
[districtList addObject:dName];
}
}</span>
<span style="font-size:14px;"> <span style="color:#009900;">//取出省字典</span>
for (NSDictionary *pDic in provinceList) {</span>
<span style="font-size:14px;"><span style="white-space:pre"> </span> <span style="color:#009900;">//省名称</span>
NSString *provinceName = [pDic objectForKey:@"name"];</span>
<span style="font-size:14px;"> <span style="white-space:pre"> </span> <span style="color:#009900;">//判断省名称</span>
if ([provinceName hasPrefix:@"山东"]) {</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建城市数组</span>
NSMutableArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//取出城市字典</span>
for (NSDictionary *cDic in cityList) {</span>
<span style="font-size:14px;"> <span style="color:#009900;">//城市名称</span>
NSString *cityName = [cDic objectForKey:@"name"];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//判断市</span>
if ([cityName hasPrefix:@"德州"]) {</span>
<span style="font-size:14px;"> <span style="color:#009900;">//创建区数组</span>
NSMutableArray *districtList = [cDic objectForKey:@"list"];</span>
<span style="font-size:14px;"> <span style="color:#009900;">//取出区名称</span>
for (NSString *dName in districtList) {
NSLog(@"%@", dName);
}
}
}
}
}</span>