iOS - OC - NSDictionary NSArray - nest

一道经典的 省市区题目 字典与数组的循环嵌套 对于初学OC的朋友理解字典与数组有很大的帮助  

 

结构图 : 

 

#import <Foundation/Foundation.h>

 

int main(int argc, const char * argv[]) {

    

    //第一版本

//    NSString * path = @"/Users/bruce_lin/Desktop/area.txt";

//    NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

//    

//    NSArray * fileArr = [content componentsSeparatedByString:@"\n"];

//    

//    NSMutableArray *mainArr = [NSMutableArray array];

//    

//    for (NSString *one in fileArr)

//    {

//        if (![one hasPrefix:@" "])

//        {

//            NSMutableDictionary * province = [NSMutableDictionary dictionary];

//            NSMutableArray * cityArr = [NSMutableArray array];

//            

//            [province setObject:cityArr forKey:one]; //为省字典添加键值 键为省名 值为市数组

//            [mainArr addObject:province];//将省字典加到省数组

//        }

//        else if ([one hasPrefix:@"  "]&& ![one hasPrefix:@"    "])

//        {

//            NSMutableDictionary * city = [NSMutableDictionary dictionary];

//            NSMutableArray * nextLevelCity = [NSMutableArray array];

//            

//            [city setObject:nextLevelCity forKey:one]; //为市字典添加键值 键为市名 值为下级城市数组

//            NSString * a = [[[mainArr lastObject]allKeys]objectAtIndex:0];// 取省数组最近一次添加的省字典的键数组 取第一个元素(市名字符串)

//            [[[mainArr lastObject]objectForKey:a]addObject:city];//将对应得值(市数组取出) 将市字典添加进去

//        }

//        else if ([one hasPrefix:@"    "])

//        {

//            //取市数组最近一次添加的市字典的值(市数组) 将下级城市名字符串添加进数组中

//            NSMutableArray *temp = [[mainArr lastObject]objectForKey:[[[mainArr lastObject]allKeys]objectAtIndex:0]];

//            [[[temp lastObject]objectForKey:[[[temp lastObject]allKeys]objectAtIndex:0]]addObject:one];

//            

//        }

//    }    

 

    //第二版本  //因为字典是无序的 打印的时候城市名有可能在下级城市之后打印 将两者代码交换下顺序即可

    NSString *content = [NSString stringWithContentsOfFile:@"/Users/bruce_lin/Desktop/area.txt" encoding:NSUTF8StringEncoding error:nil]; // 将文件内容经过转码后保存在字符串中

    

    NSArray * mainArr = [content componentsSeparatedByString:@"\n"]; //通过"\n"分割字符串 存在数组中

    

    NSMutableArray *procvinArr = [NSMutableArray array]; //建立省数组 用来保存省字典

    

    for (NSString *str in mainArr) // 遍历经过处理的文件

    {

        if (![str hasPrefix:@" "]) // 如果前缀没有空格 为省名

        {

            NSMutableDictionary * proDic = [NSMutableDictionary dictionary];

            

            NSMutableArray * cityArr = [NSMutableArray array];

            

            [proDic setObject:str forKey:@"pro"]; //省字典的第一个键值对为 pro--省名

            

            [proDic setObject:cityArr forKey:@"city"]; // 下一个键值对 city--city数组

            

            [procvinArr addObject:proDic]; // 将省字典 添加到省数组中

        }

        else if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) //如果前缀只有2个空格 没有4个空格 则为市名

        {

            NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];

            

            NSMutableArray * nextCity = [NSMutableArray array];

            

            //同上 为市字典建立两个键值对 顺序交换 打印顺序不一样

            [cityDic setObject:nextCity forKey:@"nextCityName"];

            [cityDic setObject:str forKey:@"cityName"];

            

            //根据文件结构 及遍历顺序  从省数组中 取出 最近一次添加的 省字典 通过@"city" 找到对应得值(即市数组)

            NSMutableArray * temp = [[procvinArr lastObject] objectForKey:@"city"];

            

            [temp addObject:cityDic]; //将市字典  添加到市数组中

        }

        else if ([str hasPrefix:@"    "]) // 如果有四个空格的前缀

        {

            NSMutableArray * temp1 = [[procvinArr lastObject]objectForKey:@"city"];

            

            [[[temp1 lastObject]objectForKey:@"nextCityName"]addObject:str];

            //同上 取出市字典中的下级城市数组后 将下级城市的字符串添加进数组

        }

    }

    

    //将省数组通过转码成JSON数据 保存在字符串中 即可打印出中文

    NSString * s = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:procvinArr options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];

    NSLog(@"%@",s);

    

    return 0;

}

posted @ 2015-04-05 11:31  _Lin_H  阅读(288)  评论(0编辑  收藏  举报