码迷,mamicode.com
首页 > 其他好文 > 详细

OC 运用Dictionary & Array 实现省市区分类

时间:2016-06-06 16:40:18      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

 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     }

 

OC 运用Dictionary & Array 实现省市区分类

标签:

原文地址:http://www.cnblogs.com/-Eric-Liu/p/5563881.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!