OC-可变数组-可变字典嵌套(练习-省市区)

给一个txt文档,格式为:省名前无空格, 市名前两个空格, 区名前四个空格.

文档如下图


首先要理清组数和字典的框架.实现代码如下


//
//  main.m
//  省市区
//
//  Created by dllo on 15/7/3.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSError *error = nil;
        NSString *string = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/课程代码/OC代码/省市区_班级封装作业/省市区/省市区/area.txt" encoding:NSUTF8StringEncoding error:&error];
//        NSLog(@"%@", string);
        
        NSArray *array = [string componentsSeparatedByString:@"\n"];
        
        /* 1.总数组, 对象:字典 */
        NSMutableArray *headArr = [NSMutableArray array];
        for (NSString *temp in array) {
            if (![temp hasPrefix:@" "]) {
                /* 2.总字典, key值"省名", 对应对象:省, key值"市", 对应对象: 数组 */
                NSMutableDictionary *proDic = [NSMutableDictionary dictionary];
                [proDic setObject:temp forKey:@"省名"];
                /* 3.市数组, 对象: 字典 */
                NSMutableArray *cityArr = [NSMutableArray array];
                [proDic setObject:cityArr forKey:@"市"];
                [headArr addObject:proDic];
            }
            else if (![temp hasPrefix:@"    "])
            {
                /* 4.字典,  key值"市名", 对应对象:市, key值"区", 对应对象: 数组*/
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                [cityDic setObject:temp forKey:@"市名"];
                /* 5.区数组, 对象:区 */
                NSMutableArray *districtArr = [NSMutableArray array];
                [cityDic setObject:districtArr forKey:@"区"];
                
                NSMutableDictionary *proDic = [headArr lastObject];/* 最后一个省 */
                NSMutableArray *cityArr = [proDic objectForKey:@"市"];/* 最后一个省对应的市 */
                
                [cityArr addObject:cityDic];
            }
            else
            {
                NSMutableDictionary *proDic = [headArr lastObject]; /* 获取最后的省字典 */
                
                NSMutableArray *cityArr = [proDic objectForKey:@"市"];/* 获取市的数组 */
                
                NSMutableDictionary *cityDic = [cityArr lastObject];/* 获取市字典 */
               
                NSMutableArray *districtArr = [cityDic objectForKey:@"区"]; /* 获取区的数组 */
            
                [districtArr addObject:temp];/* 把区名加到数组中 */
            }
        }
        
        /* 遍历输出 */
        for (NSMutableDictionary *proDic in headArr) {
            NSLog(@"%@", [proDic objectForKey:@"省名"]);
            NSMutableArray *cityArr =[proDic objectForKey:@"市"];
            for (NSMutableDictionary *cityDic in cityArr) {
                NSLog(@"%@", [cityDic objectForKey:@"市名"]);
                NSMutableArray *districtArr = [cityDic objectForKey:@"区"];
                for (NSString *dis in districtArr) {
                    NSLog(@"%@", dis);
                }
            }
        }
    }
    return 0;
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuling962464/article/details/46793777
个人分类: Objective-C iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭