OC NSDictionary 省市区 练习
2015-08-12 14:42
225 查看
#import <Foundation/Foundation.h>
int main(int argc, const char *
argv[]) {
NSString *string = [[NSString alloc] initWithContentsOfFile:@"/Users/dllo/Downloads/area.txt"encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@", string);
// 以 @"\n" 分割字符串 放入数组
NSArray *array = [string componentsSeparatedByString:@"\n"];
//NSLog(@"%@", array);
//遍历数组
//省数组
NSMutableArray *province = [NSMutableArray arrayWithCapacity:0];
//市数组
NSMutableArray *citys = [NSMutableArray array];
//区域数组
NSMutableArray *areas = [NSMutableArray array];
//省字典
NSMutableDictionary *pro;
//市字典
NSMutableDictionary *city;
for (NSString *string in array) {
/***********************************
判断逻辑:当前面有四个空格的时候为区(if)
当前面有两个空格的时候为市(else if)
其余情况为省(else)
**********************************/
if ([string hasPrefix:@" "]) {
//判断是否为最后一个元素
if ([array indexOfObject:string] == (array.count - 1))
{
//判断省字典是否存在
if (pro) {
//将数组保存至字典
[pro setObject:citys forKey:@"city"];
}
//判断市字典是否存在
if (city) {
[city setObject:areas forKey:@"area"];
}
}
//截取区域字符串的前面的两个空格
NSString *otherString = [string substringFromIndex:4];
//分割 字符串
NSArray *array = [otherString componentsSeparatedByString:@"
"];
//定义区字典
NSMutableDictionary *area = [NSMutableDictionarydictionaryWithObjectsAndKeys:[array objectAtIndex:0],[array objectAtIndex:1], nil];
//将区字典添加到区数组
[areas addObject:area];
} else if ([string hasPrefix:@"
"]) {
if (city) {
//将区字典加入市字典
[city setObject:areas forKey:@"area"];
//初始化区数组
areas = [NSMutableArray array];
}
//截取掉市字符串前面两个空格
NSString *otherString = [string substringFromIndex:2];
//把市字符串和编号分开
NSArray *array = [otherString componentsSeparatedByString:@"
"];
//初始化市字典
city = [NSMutableDictionary dictionaryWithObjectsAndKeys:[array objectAtIndex:0],
[array objectAtIndex:1], nil];
//将市字典填入市数组
[citys addObject:city];
} else {
//判断省字典是否存在
if (pro) {
//将数组保存至字典
[pro setObject:citys forKey:@"city"];
//重新给城市空间数组
citys = [NSMutableArray array];
}
//把省和编号分开
NSArray *array = [string componentsSeparatedByString:@"
"];
pro = [NSMutableDictionary dictionary];//初始化省字典
[pro setObject:[array objectAtIndex:0] forKey:[array objectAtIndex:1]];
//将省字典加入省数组
[province addObject:pro];
}
}
NSLog(@"%@", province);
return 0;
}
int main(int argc, const char *
argv[]) {
NSString *string = [[NSString alloc] initWithContentsOfFile:@"/Users/dllo/Downloads/area.txt"encoding:NSUTF8StringEncoding error:nil];
//NSLog(@"%@", string);
// 以 @"\n" 分割字符串 放入数组
NSArray *array = [string componentsSeparatedByString:@"\n"];
//NSLog(@"%@", array);
//遍历数组
//省数组
NSMutableArray *province = [NSMutableArray arrayWithCapacity:0];
//市数组
NSMutableArray *citys = [NSMutableArray array];
//区域数组
NSMutableArray *areas = [NSMutableArray array];
//省字典
NSMutableDictionary *pro;
//市字典
NSMutableDictionary *city;
for (NSString *string in array) {
/***********************************
判断逻辑:当前面有四个空格的时候为区(if)
当前面有两个空格的时候为市(else if)
其余情况为省(else)
**********************************/
if ([string hasPrefix:@" "]) {
//判断是否为最后一个元素
if ([array indexOfObject:string] == (array.count - 1))
{
//判断省字典是否存在
if (pro) {
//将数组保存至字典
[pro setObject:citys forKey:@"city"];
}
//判断市字典是否存在
if (city) {
[city setObject:areas forKey:@"area"];
}
}
//截取区域字符串的前面的两个空格
NSString *otherString = [string substringFromIndex:4];
//分割 字符串
NSArray *array = [otherString componentsSeparatedByString:@"
"];
//定义区字典
NSMutableDictionary *area = [NSMutableDictionarydictionaryWithObjectsAndKeys:[array objectAtIndex:0],[array objectAtIndex:1], nil];
//将区字典添加到区数组
[areas addObject:area];
} else if ([string hasPrefix:@"
"]) {
if (city) {
//将区字典加入市字典
[city setObject:areas forKey:@"area"];
//初始化区数组
areas = [NSMutableArray array];
}
//截取掉市字符串前面两个空格
NSString *otherString = [string substringFromIndex:2];
//把市字符串和编号分开
NSArray *array = [otherString componentsSeparatedByString:@"
"];
//初始化市字典
city = [NSMutableDictionary dictionaryWithObjectsAndKeys:[array objectAtIndex:0],
[array objectAtIndex:1], nil];
//将市字典填入市数组
[citys addObject:city];
} else {
//判断省字典是否存在
if (pro) {
//将数组保存至字典
[pro setObject:citys forKey:@"city"];
//重新给城市空间数组
citys = [NSMutableArray array];
}
//把省和编号分开
NSArray *array = [string componentsSeparatedByString:@"
"];
pro = [NSMutableDictionary dictionary];//初始化省字典
[pro setObject:[array objectAtIndex:0] forKey:[array objectAtIndex:1]];
//将省字典加入省数组
[province addObject:pro];
}
}
NSLog(@"%@", province);
return 0;
}
相关文章推荐
- Rational Rose 的安装
- 位bit,字节byte,K,M,G(转)
- linux下的DB2数据库表结构和数据分开的备份,还原到windows下,环境如下
- VCPP/CPP/PHP_统计图插件-ChartDirector(ASP/COM/VB/.NET/JSP/Java/ColdFusion/Perl/Python/Ruby)
- Elasticsearch自动分片
- 测试图片问题
- 测试保存
- 文件导入
- 树形DP UVA 1292 Strategic game
- How-to: set yarn mapreduce memory properties
- Java程序员面试题集(151-180)
- [BZOJ1660] [Usaco2006 Nov]Bad Hair Day 乱发节
- Android之TabHost布局
- ios8原生库实现扫描相册内二维码图片
- MFC文件操作大全 2
- thinkPHP 中去除URL中的index.php
- Android View系统学习——从setContentView说开来
- PHP 5.3以上版本推荐使用mysqlnd驱动
- Spark中的GraphX
- jQuery_统计图插件-chart.js