OC-字典-省市区-省数组套省字典套市数组套市字典套区数组

省市区
OC-字典-省市区-省数组套省字典套市数组套市字典套区数组
数据文件:省市区TXT(数据)文件

 NSString *str = [NSString stringWithContentsOfFile:@"area.txt" encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@", str);
NSArray *arr = [str componentsSeparatedByString:@"\n"];
NSLog(@"%@", arr);
// 创建省数组
NSMutableArray *sArr = [NSMutableArray array];
// 遍历
for (NSString *temp  in arr) {
    if (![temp hasPrefix:@"  "]) {
       // 创建省字典
        NSMutableDictionary *sDic = [NSMutableDictionary dictionary];
        // 添加省名字
        [sDic setObject:temp forKey:@"Sname"];
        // 添加市数组
        [sDic setObject:[NSMutableArray array] forKey:@"cities"];
        // 省字典添加到省数组
        [sArr addObject:sDic];
    }
    if ([temp hasPrefix:@"  "] && [temp hasPrefix:@"   "] ) {
        // 创建市字典
        NSMutableDictionary *CitDic = [NSMutableDictionary dictionary];
        // 添加市名
        [CitDic setObject:temp forKey:@"Cname"];
        // 添加区数组
        [CitDic setObject:[NSMutableArray array] forKey:@"disname"];
        // 获取当前省
        NSMutableDictionary *sDic = sArr.lastObject;
        // 获取市数组
        NSMutableArray *cArr = [sDic objectForKey:@"cities"];
        // 区数组添加到市数组
        [cArr addObject:CitDic];
        
    }
    if ([temp hasPrefix:@"    "]) {
        // 获取当前省
        NSMutableDictionary *sDic = sArr.lastObject;
        // 获取当前市数组
        NSMutableArray *cArr = [sDic objectForKey:@"cities"];
        // 市字典
        NSMutableDictionary *cDic = cArr.lastObject;
        // 添加区数组
        NSMutableArray *disArr = [cDic objectForKey:@"disname"];
        //
        [disArr addObject:cArr];
        
        
    }
    
    NSLog(@"%@", temp);
}

屏幕快照 2016-01-06 上午10.43.31.png
屏幕快照 2016-01-06 上午10.43.50.png
屏幕快照 2016-01-06 上午10.44.42.png

标签: none

添加新评论

Title - Artist
0:00