NSString *filePath = @"/Users/dlios/Downloads/area.txt";
NSError *error = nil;
NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"error : %@", [error localizedDescription]);
exit(1);
}
NSLog(@"%@", buffer);
NSArray *dataArray = [buffer componentsSeparatedByString:@"\n"];
NSLog(@"%@", dataArray);
NSMutableArray *provinces = [NSMutableArray array];
for (NSString *s in dataArray) {
// 找到 省
if (![s hasPrefix:@" "]) {
NSMutableArray *citys = [NSMutableArray array];
NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", citys, @"cities", nil];
[provinces addObject:pDic];
}
// 找到 城市
if ([s hasPrefix:@" "] && ![s hasPrefix:@" "]) {
NSMutableArray *areas = [NSMutableArray array];
NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", areas, @"areas", nil];
NSDictionary *pDic = [provinces lastObject];
NSMutableArray *cities = [pDic objectForKey:@"cities"];
[cities addObject:cDic];
}
// 找到 区
if ([s hasPrefix:@" "]) {
NSDictionary *pDic = [provinces lastObject];
NSArray *cities = [pDic objectForKey:@"cities"];
NSDictionary *cDic = [cities lastObject];
NSMutableArray *areas = [cDic objectForKey:@"areas"];
[areas addObject:s];
}
}
NSLog(@"%@", provinces);
for (NSDictionary *pDic in provinces) {
if ([[pDic objectForKey:@"name"] hasPrefix:@"山东"]) {
NSArray *cities = [pDic objectForKey:@"cities"];
for (NSDictionary *cDic in cities) {
if ([[cDic objectForKey:@"name"] hasPrefix:@" 烟台"]) {
NSArray *areas = [cDic objectForKey:@"areas"];
for (NSString *areaName in areas) {
NSLog(@"%@", areaName);
}
}
}
}
}