標籤:
一道經典的 省市區題目 字典與數組的迴圈嵌套 對於初學OC的朋友理解字典與數組有很大的協助
結構圖 :
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//第一版本
// NSString * path = @"/Users/bruce_lin/Desktop/area.txt";
// NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//
// NSArray * fileArr = [content componentsSeparatedByString:@"\n"];
//
// NSMutableArray *mainArr = [NSMutableArray array];
//
// for (NSString *one in fileArr)
// {
// if (![one hasPrefix:@" "])
// {
// NSMutableDictionary * province = [NSMutableDictionary dictionary];
// NSMutableArray * cityArr = [NSMutableArray array];
//
// [province setObject:cityArr forKey:one]; //為省字典添加索引值 鍵為省名 值為市數組
// [mainArr addObject:province];//將省字典加到省數組
// }
// else if ([one hasPrefix:@" "]&& ![one hasPrefix:@" "])
// {
// NSMutableDictionary * city = [NSMutableDictionary dictionary];
// NSMutableArray * nextLevelCity = [NSMutableArray array];
//
// [city setObject:nextLevelCity forKey:one]; //為市字典添加索引值 鍵為市名 值為下級城市數組
// NSString * a = [[[mainArr lastObject]allKeys]objectAtIndex:0];// 取省數組最近一次添加的省字典的鍵數組 取第一個元素(市名字串)
// [[[mainArr lastObject]objectForKey:a]addObject:city];//將對應得值(市數組取出) 將市字典添加進去
// }
// else if ([one hasPrefix:@" "])
// {
// //取市數組最近一次添加的市字典的值(市數組) 將下級城市名字串添加進數組中
// NSMutableArray *temp = [[mainArr lastObject]objectForKey:[[[mainArr lastObject]allKeys]objectAtIndex:0]];
// [[[temp lastObject]objectForKey:[[[temp lastObject]allKeys]objectAtIndex:0]]addObject:one];
//
// }
// }
//第二版本 //因為字典是無序的 列印的時候城市名有可能在下級城市之後列印 將兩者代碼交換下順序即可
NSString *content = [NSString stringWithContentsOfFile:@"/Users/bruce_lin/Desktop/area.txt" encoding:NSUTF8StringEncoding error:nil]; // 將檔案內容經過轉碼後儲存在字串中
NSArray * mainArr = [content componentsSeparatedByString:@"\n"]; //通過"\n"分割字串 存在數組中
NSMutableArray *procvinArr = [NSMutableArray array]; //建立省數組 用來儲存省字典
for (NSString *str in mainArr) // 遍曆經過處理的檔案
{
if (![str hasPrefix:@" "]) // 如果首碼沒有空格 為省名
{
NSMutableDictionary * proDic = [NSMutableDictionary dictionary];
NSMutableArray * cityArr = [NSMutableArray array];
[proDic setObject:str forKey:@"pro"]; //省字典的第一個索引值對為 pro--省名
[proDic setObject:cityArr forKey:@"city"]; // 下一個索引值對 為 city--city數組
[procvinArr addObject:proDic]; // 將省字典 添加到省數組中
}
else if ([str hasPrefix:@" "] && ![str hasPrefix:@" "]) //如果首碼只有2個空格 沒有4個空格 則為市名
{
NSMutableDictionary *cityDic = [NSMutableDictionary dictionary];
NSMutableArray * nextCity = [NSMutableArray array];
//同上 為市字典建立兩個索引值對 順序交換 列印順序不一樣
[cityDic setObject:nextCity forKey:@"nextCityName"];
[cityDic setObject:str forKey:@"cityName"];
//根據檔案結構 及遍曆順序 從省數組中 取出 最近一次添加的 省字典 通過@"city"鍵 找到對應得值(即市數組)
NSMutableArray * temp = [[procvinArr lastObject] objectForKey:@"city"];
[temp addObject:cityDic]; //將市字典 添加到市數組中
}
else if ([str hasPrefix:@" "]) // 如果有四個空格的首碼
{
NSMutableArray * temp1 = [[procvinArr lastObject]objectForKey:@"city"];
[[[temp1 lastObject]objectForKey:@"nextCityName"]addObject:str];
//同上 取出市字典中的下級城市數組後 將下級城市的字串添加進數組
}
}
//將省數組通過轉碼成JSON資料 儲存在字串中 即可列印出中文
NSString * s = [[NSString alloc]initWithData:[NSJSONSerialization dataWithJSONObject:procvinArr options:NSJSONWritingPrettyPrinted error:nil] encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
return 0;
}
iOS - OC - NSDictionary NSArray - nest