//
// 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
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...