美文网首页
Objective-C-省份城市地区(17-08-02)Mine

Objective-C-省份城市地区(17-08-02)Mine

作者: Hilarylii | 来源:发表于2017-08-02 19:56 被阅读0次
    //
    //  main.m
    //  yan1
    //
    //  Created by lanou3g on 17/8/2.
    //  Copyright © 2017年 lanou3g. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    int main(int argc, const char * argv[]) {
        
        //创建省份数组
        NSMutableArray *provinceArray = [NSMutableArray array];
        //加载文件得到字符串
        NSString *contentString = [NSString stringWithContentsOfFile:@"/Users/lanou3g/Downloads/area.txt" encoding:NSUTF8StringEncoding error:nil];
        //用\n分割字符串,得到每一行的字符串内容
        NSArray * lineArray = [contentString componentsSeparatedByString:@"\n"];
        for (NSString *lineString in lineArray) {
            if (![lineString hasPrefix:@" "]) { //前缀不是空格的话,找省份名
            //创建省份字典 provinceDic={@“provinceName”:@“北京” , @“cityArray”:@“[各个城市]”}
                NSMutableDictionary *provinceDic = [NSMutableDictionary dictionary];
                [provinceDic setObject:lineString forKey:@"provinceName"];
                NSMutableArray *cityEvery = [NSMutableArray array];
                [provinceDic setObject:cityEvery forKey:@"cityArray"];
                [provinceArray addObject:provinceDic];
            }else if(![lineString hasPrefix:@"    "]) { //找城市
                NSMutableDictionary *provinceDic = [provinceArray lastObject];
                NSMutableArray *cityEvery = [provinceDic objectForKey:@"cityArray"];
                NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
                [cityDic setObject:lineString forKey:@"cityName"];
                NSMutableArray *areaEvery = [NSMutableArray array];
                [cityDic setObject:areaEvery forKey:@"areaArray"];
                [cityEvery addObject:cityDic];
            }else {//找地区
                //获取当前地区所属的省份
                NSMutableDictionary *provinceDic = [provinceArray lastObject];
                //获取该省份的城市数组
                NSMutableArray *cityEvery = [provinceDic objectForKey:@"cityArray"];
                //从城市数组中取出当前地区所属的城市
                NSMutableDictionary *cityDic = [cityEvery lastObject];
                //获取该城市的地区数组
                NSMutableArray *areaEvery = [cityDic objectForKey:@"areaArray"];
                [areaEvery addObject:lineString];
                
            }
        }
        
        for (NSMutableDictionary *provinceDic in provinceArray) {
            NSLog(@"省份:%@",[provinceDic objectForKey:@"provinceName"]);
            for (NSMutableDictionary *cityDic in [provinceDic objectForKey:@"cityArray"]) {
                NSLog(@"%@",[cityDic objectForKey:@"cityName"]);
                for (NSString *areaName in [cityDic objectForKey:@"areaArray"]) {
                    NSLog(@"%@",areaName);
                }
            }
        }
        
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:Objective-C-省份城市地区(17-08-02)Mine

          本文链接:https://www.haomeiwen.com/subject/xavylxtx.html