标签:
题目:解析根据文件里面的内容,读入一个城市,输出所在首字母
比如读入 长春 输出 c
读入 北京 输出 b
从如下文件解析
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 3 <plist version="1.0"> 4 <dict> 5 <key>a</key> 6 <array> 7 <string>安吉</string> 8 <string>安庆</string> 9 <string>鞍山</string> 10 <string>澳门</string> 11 </array> 12 <key>b</key> 13 <array> 14 <string>保定</string> 15 <string>宝鸡</string> 16 <string>包头</string> 17 <string>北海</string> 18 <string>北京</string> 19 <string>蚌埠</string> 20 <string>滨州</string> 21 </array> 22 <key>c</key> 23 <array> 24 <string>沧州</string> 25 <string>长春</string> 26 <string>常德</string> 27 <string>长乐</string> 28 <string>长沙</string> 29 <string>常熟</string> 30 <string>常州</string> 31 <string>潮阳</string> 32 <string>潮州</string> 33 <string>承德</string> 34 <string>成都</string> 35 <string>郴州</string> 36 <string>重庆</string> 37 <string>滁州</string> 38 <string>慈禧</string> 39 <string>从化</string> 40 </array> 41 </dict> 42 </plist>
本次思路
1.创建一个首字母和城市名称的一个类FirstLetterAndCity,其中有两个成员变量,一个位字符串类型,存储首字母,一个动态数组,存储该字母对应的所有城市。
1 // 2 // FirstLetterAndCity.h 3 // day16_OC5_城市首字母 4 // 5 // Created by qianfeng on 15/6/15. 6 // Copyright (c) 2015年 QF_Lpj. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface FirstLetterAndCity : NSObject 12 { 13 NSString *_firstLetter; //城市首字母 14 NSMutableArray *_cityList; //城市集合 15 } 16 - (id)init; 17 18 //设置城市首字母 19 - (void)setFirstLetter:(NSString *)firstLetter; 20 - (NSString *)firstLetter; 21 22 //首字母和城市类的城市列表中添加城市 23 - (void)addCityList:(NSString *)cityName; 24 - (NSMutableArray *)cityList; 25 26 27 @end 28 ------------------------------ 29 // 30 // FirstLetterAndCity.m 31 // day16_OC5_城市首字母 32 // 33 // Created by qianfeng on 15/6/15. 34 // Copyright (c) 2015年 QF_Lpj. All rights reserved. 35 // 36 37 #import "FirstLetterAndCity.h" 38 39 @implementation FirstLetterAndCity 40 41 - (id)init 42 { 43 if (self = [super init]) { 44 _firstLetter = @""; 45 _cityList = [[NSMutableArray alloc] init]; 46 } 47 return self; 48 } 49 50 //设置城市首字母 51 - (void)setFirstLetter:(NSString *)firstLetter 52 { 53 _firstLetter = firstLetter; 54 } 55 - (NSString *)firstLetter 56 { 57 return _firstLetter; 58 } 59 60 //首字母和城市类的城市列表中添加城市 61 - (void)addCityList:(NSString *)cityName 62 { 63 if (![cityName isEqualToString:@""]) { 64 [self ->_cityList addObject:cityName]; 65 } 66 } 67 - (NSMutableArray *)cityList 68 { 69 return _cityList; 70 } 71 72 73 @end
2.创建一个城市管理类,其中有一个动态数组的成员变量,用于存储所有FirstLetterAndCity类对象。
之后通过遍历管理类中的数组,对每一个FirstLetterAndCity对象中的数组再意义遍历,当对应的城市名称在其中有时,就返回该对象对应的城市首字母
1 // 2 // ManagerCity.h 3 // day16_OC5_城市首字母 4 // 5 // Created by qianfeng on 15/6/15. 6 // Copyright (c) 2015年 QF_Lpj. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "FirstLetterAndCity.h" 11 12 #define FilePath @"/Users/qianfeng/Desktop/day16_OC5_复合类设计/day16_OC5_城市首字母/城市首字母.txt" 13 14 @interface ManagerCity : NSObject 15 { 16 NSMutableArray *_firstLetterAndCityList; //用于存储城市集合和首字母 FirstLetterAndCity 对象 17 } 18 19 - (id)init; 20 21 - (NSString *)getFileAllString:(NSString *)filePath; 22 23 //输入一个城市名称,返回对应的首字母 24 - (NSString *)getFirstLetterByCityName:(NSString *)cityName; 25 26 27 + (void)testManagerCity:(NSString *)cityName; 28 29 30 31 @end 32 33 --------------------------------------------------- 34 // 35 // ManagerCity.m 36 // day16_OC5_城市首字母 37 // 38 // Created by qianfeng on 15/6/15. 39 // Copyright (c) 2015年 QF_Lpj. All rights reserved. 40 // 41 42 #import "ManagerCity.h" 43 #import "FirstLetterAndCity.h" 44 45 //声明一个匿名类别 实现私有化 46 @interface ManagerCity () 47 48 - (NSString *)getFLetterByCityName:(NSString *)cityName; 49 50 @end 51 52 @implementation ManagerCity 53 54 - (id)init 55 { 56 if (self = [super init]) { 57 _firstLetterAndCityList = [[NSMutableArray alloc] init]; 58 } 59 return self; 60 } 61 62 //由路径生成一个字符串对象 63 - (NSString *)getFileAllString:(NSString *)filePath 64 { 65 NSString *fileContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 66 return fileContent; 67 } 68 69 //通过城市名称获取城市首字母 70 - (NSString *)getFirstLetterByCityName:(NSString *)cityName 71 { 72 NSString *fileContent = [self getFileAllString:FilePath]; 73 NSArray *fileContentArray = [fileContent componentsSeparatedByString:@"\n"]; 74 //首字母 城市名类 75 FirstLetterAndCity *letterCity; 76 if (fileContentArray) { 77 for (NSString *item in fileContentArray) { 78 if ([item isEqualToString:@""]) { 79 continue; 80 } 81 else if([item containsString:@"<key>"]) 82 { 83 letterCity = [[FirstLetterAndCity alloc] init]; 84 //<key>x</key> 获取‘x‘前后的位置坐标,由此获取到x 85 NSRange rangeRight = [item rangeOfString:@">"]; 86 NSRange rangeLeft = [item rangeOfString:@"</"]; 87 [letterCity setFirstLetter:[item substringWithRange:NSMakeRange(rangeRight.location+1, rangeLeft.location-rangeRight.location-1)]]; 88 89 }else if([item containsString:@"<string>"]) 90 { 91 //获取"<string>XX</string>" XX之间的位置,并且由此获得对应的城市 92 NSRange rangeRight = [item rangeOfString:@">"]; 93 NSRange rangeLeft = [item rangeOfString:@"</"]; 94 [letterCity addCityList:[item substringWithRange:NSMakeRange(rangeRight.location+1, rangeLeft.location-rangeRight.location-1)]]; 95 } 96 //只有当碰到"</array>"时才说明走完了一个key 和 一列 string 97 if (letterCity && [item containsString:@"</array>"]) { 98 [self->_firstLetterAndCityList addObject:letterCity]; 99 } 100 } 101 } 102 103 NSString *firstLetter = [self getFLetterByCityName:cityName]; 104 105 106 return firstLetter; 107 } 108 109 //当获取 _firstLetterAndCityList 列表时候,通过传入的城市名字获取到对应的城市首字母 110 - (NSString *)getFLetterByCityName:(NSString *)cityName 111 { 112 //FirstLetterAndCity *flCity = [[FirstLetterAndCity alloc] init]; 113 for (FirstLetterAndCity *flCity in self->_firstLetterAndCityList) { 114 for (NSString *item in [flCity cityList]) { 115 if ([item isEqualToString:cityName]) { 116 return [flCity firstLetter]; 117 } 118 } 119 } 120 return @"NotFound"; 121 } 122 123 124 + (void)testManagerCity:(NSString *)cityName 125 { 126 ManagerCity *mCity = [[ManagerCity alloc] init]; 127 NSString *firstLetter = [mCity getFirstLetterByCityName:cityName]; 128 if (![firstLetter isEqualToString:@"NotFound"]) { 129 NSLog(@"%@",firstLetter); 130 }else{ 131 NSLog(@"没有%@城市的首字母",cityName); 132 } 133 } 134 @end
标签:
原文地址:http://www.cnblogs.com/BeyondAverage0908/p/4579165.html