省市区存储数据

样本

北京 1
  北京市 1
    西城区 1
    东城区 2
    ....
天津 2
  天津市 2
    和平区 19
    河东区 20
    河西区 21
    ...
河北 3
  石家庄市 3
    长安区 37
    桥东区 38
    ...
  唐山市 4
    路北区 60
    路南区 61
    古冶区 62
    开平区 63
    ...
...

code

@property (nonatomic, retain) NSMutableArray *proArr;
@property (nonatomic, retain) NSMutableArray *cityArr;
@property (nonatomic, retain) NSMutableArray *zoneArr;

NSString *path = @"/Users/dllo/Desktop/UI_蓝欧/UI11_多个UITableView练习/UI11_多个UITableView练习/area.txt";
    // 根据路径产生相应的字符串
    NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 对字符串逐行进行切割
    NSArray *strArr = [str componentsSeparatedByString:@"\n"];

    self.proArr = [NSMutableArray array];

    for (NSString *temp in strArr) {
        if (![temp hasPrefix:@" "]) {
            NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
            NSMutableArray *cityArr = [NSMutableArray array];
            [proDic setObject:temp forKey:@"proName"];
            [proDic setObject:cityArr forKey:@"cityArr"];
            [self.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 = [self.proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            [cityArr addObject:cityDic];
        }else{
            NSMutableDictionary *proDic = [self.proArr lastObject];
            NSMutableArray *cityArr = proDic[@"cityArr"];
            NSMutableDictionary *cityDic = [cityArr lastObject];
            NSMutableArray *zoneArr = cityDic[@"zoneArr"];
            [zoneArr addObject:temp];
        }
    }

你可能感兴趣的:(iOS-数据解析)