YIem`s Blog -心比天高命比纸薄-链接找不到的请在站内搜索内容!

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);
}



当前页面是本站的「Google AMP」版。查看和发表评论请点击:完整版 »