1 NSString *filePath = @"/Users/dlios/Downloads/area.txt"; 2 NSError *error = nil; 3 NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; 4 if (error) { 5 NSLog(@"error : %@", [error localizedDescription]); 6 exit(1); 7 } 8 NSLog(@"%@", buffer); 9 NSArray *dataArray = [buffer componentsSeparatedByString:@"\n"]; 10 NSLog(@"%@", dataArray); 11 NSMutableArray *provinces = [NSMutableArray array]; 12 for (NSString *s in dataArray) { 13 // 找到 省 14 if (![s hasPrefix:@" "]) { 15 NSMutableArray *citys = [NSMutableArray array]; 16 NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", citys, @"cities", nil]; 17 [provinces addObject:pDic]; 18 } 19 // 找到 城市 20 if ([s hasPrefix:@" "] && ![s hasPrefix:@" "]) { 21 NSMutableArray *areas = [NSMutableArray array]; 22 NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:s, @"name", areas, @"areas", nil]; 23 NSDictionary *pDic = [provinces lastObject]; 24 NSMutableArray *cities = [pDic objectForKey:@"cities"]; 25 [cities addObject:cDic]; 26 } 27 // 找到 区 28 if ([s hasPrefix:@" "]) { 29 NSDictionary *pDic = [provinces lastObject]; 30 NSArray *cities = [pDic objectForKey:@"cities"]; 31 NSDictionary *cDic = [cities lastObject]; 32 NSMutableArray *areas = [cDic objectForKey:@"areas"]; 33 34 [areas addObject:s]; 35 } 36 } 37 NSLog(@"%@", provinces); 38 39 for (NSDictionary *pDic in provinces) { 40 if ([[pDic objectForKey:@"name"] hasPrefix:@"山东"]) { 41 NSArray *cities = [pDic objectForKey:@"cities"]; 42 for (NSDictionary *cDic in cities) { 43 if ([[cDic objectForKey:@"name"] hasPrefix:@" 烟台"]) { 44 NSArray *areas = [cDic objectForKey:@"areas"]; 45 for (NSString *areaName in areas) { 46 NSLog(@"%@", areaName); 47 } 48 } 49 } 50 } 51 }
时间: 2024-09-22 21:37:34