//
// main.m
// OC05_数组省市区
//
// Created by dllo on 15/7/21.
// Copyright (c) 2015年 Clare. All rights reserved.
//
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
// 先找到文件对应的路径
NSString *path = @"/Users/dllo/Desktop/Clare/OC/OC05_数组省市区/OC05_数组省市区/area.txt";
// 通过路径产生字符串,内容就是txt文本的内容
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@", str);
// 对整体的字符串进行分割
// 以"\n"作为标识,将字符串分割为小的字符串,保存到一个大的数组中
NSArray *strArr = [str componentsSeparatedByString:@"\n"];
// // 对strArr进行遍历
// for (NSString *str in strArr) {
// NSLog(@"%@", str);
// }
// NSLog(@"%ld",strArr.count);
// 把所有的省的字符串放到一个数组中
// 创建一个空的,可变的数组,用来装所有符合条件的省字符串
NSMutableArray *proArr = [NSMutableArray array];
// for (NSString *str in strArr) {
// // 如果没有空格就是省
// if (![str hasPrefix:@" "]) {
// [proArr addObject:str];
// }
// }
// for (NSString *str in proArr) {
// NSLog(@"%@", str);
// }
// 通过一个大数组,里面放31个元素,一个省对应了一个元素,然后每个元素也是一个数组,这个数组保存了省的所有的城市名的数组
// for (NSString *str in strArr) {
// // 只要碰到省对应的字符串,就创建一个可变的数组
// if (![str hasPrefix:@" "]) {
// NSMutableArray *cityArr = [NSMutableArray array];
// // 把创建的数组放到省数组中
// [proArr addObject:cityArr];
// } else if ([str hasPrefix:@" "] && ![str hasPrefix:@" "]) {
// // 通过lastobject获取要装字符串的市数组
// NSMutableArray *cityArr = [proArr lastObject];
// // 把城市的字符串放到数组中
// [cityArr addObject:str];
// }
// }
// for (NSMutableArray *strArr in proArr) {
// NSLog(@"\n");
// NSLog(@"%@", strArr);
// for (NSString *str in strArr) {
// NSLog(@"%@", str);
// }
// }
for (NSString *str in strArr) {
// 只要碰到省对应的字符串,就创建一个可变的数组
if (![str hasPrefix:@" "]) {
NSMutableArray *cityArr = [NSMutableArray array];
// 把创建的数组放到省数组中
[proArr addObject:cityArr];
} else if ([str hasPrefix:@" "] && ![str hasPrefix:@" "]) {
// 通过lastobject获取要装字符串的市数组
NSMutableArray *cityArr = [proArr lastObject];
NSMutableArray *districtArr = [NSMutableArray array];
// 把城市的字符串放到数组中
[cityArr addObject:districtArr];
} else if ([str hasPrefix:@" "]) {
NSMutableArray *cityArr = [proArr lastObject];
NSMutableArray *districtArr = [cityArr lastObject];
[districtArr addObject:str];
}
}
// for (NSString *str in proArr) {
// NSLog(@"%@", str);
// }
for (NSArray *strArr in proArr) {
NSLog(@"****************省******************");
//NSLog(@"%@", proArr);
for (NSArray *cityArr in strArr) {
NSLog(@"----------------市------------------");
for (NSString *str in cityArr) {
NSLog(@"%@", str);
}
}
}
return 0;
}