首页 > 代码库 > OC 获取城市首字母

OC 获取城市首字母

解析根据文件里面的内容,读入一个城市,输出所在首字母 

比如读入 长春 输出 c

读入 北京 输出 b

不知道文本中的字体是什么格式 读取文件时 程序不能正确运行

运用oc中的字典

initWithObjectsAndKeys  初始化 用值和键
        // 注意这里要输入一个文件 这里是argv[1]
        NSString *path = [NSString stringWithFormat:@"%s", argv[1]];


#import <Foundation/Foundation.h>
int main(int argc,const char *argv[])
#define PATH @"/Users/qianfeng/Desktop/Feng/qfile-6.txt"
{
    @autoreleasepool {
        //   NSDictionary *dict = [[NSDictionary alloc]  initWithContentsOfFile:PATH];  //从文本中获取
        NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"安徽",@"a",@"北京",@"b",@"沧州",@"c", nil];
        //        NSDictionary *dict = [[NSDictionary alloc] init];
        
        NSArray *values = [dict allKeys]; // 遍历所有键 对应还有遍历所有值 allValues
        char str[1024];
        scanf("%s",str);
        NSString *city = [[NSString alloc] initWithUTF8String:str];  
        
        
        // NSLog(@"%@",city);
        for(NSString *key in values)
        {
            if([city isEqualToString:[dict objectForKey:key]])  //根据键查找对应值
            {
                NSLog(@"%@",key);
            }
        }
    }
    return 0;
}

qfile-6.txt

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>a</key>
	<array>
		<string>安吉</string>
		<string>安庆</string>
		<string>鞍山</string>
		<string>澳门</string>
	</array>
	<key>b</key>
	<array>
		<string>保定</string>
		<string>宝鸡</string>
		<string>包头</string>
		<string>北海</string>
		<string>北京</string>
		<string>蚌埠</string>
		<string>滨州</string>
	</array>
	<key>c</key>
	<array>
		<string>沧州</string>
		<string>长春</string>
		<string>常德</string>
		<string>长乐</string>
		<string>长沙</string>
		<string>常熟</string>
		<string>常州</string>
		<string>潮阳</string>
		<string>潮州</string>
		<string>承德</string>
		<string>成都</string>
		<string>郴州</string>
		<string>重庆</string>
		<string>滁州</string>
		<string>慈禧</string>
		<string>从化</string>
	</array>
	<key>d</key>
	<array>
		<string>大连</string>
		<string>丹东</string>
		<string>大庆</string>
		<string>大同</string>
		<string>德阳</string>
		<string>德州</string>
		<string>东莞</string>
		<string>东营</string>
	</array>
	<key>e</key>
	<array>
		<string>鄂尔多斯</string>
		<string>鄂州</string>
	</array>
	<key>f</key>
	<array>
		<string>奉化</string>
		<string>佛山</string>
		<string>抚顺</string>
		<string>阜阳</string>
		<string>福州</string>
	</array>
	<key>g</key>
	<array>
		<string>赣州</string>
		<string>广州</string>
		<string>桂林</string>
		<string>贵阳</string>
	</array>
	<key>h</key>
	<array>
		<string>哈尔滨</string>
		<string>海口</string>
		<string>海拉尔</string>
		<string>邯郸</string>
		<string>杭州</string>
		<string>合肥</string>
		<string>鹤岗</string>
		<string>衡阳</string>
		<string>鹤山</string>
		<string>河源</string>
		<string>花都</string>
		<string>淮安</string>
		<string>淮北</string>
		<string>淮南</string>
		<string>黄石</string>
		<string>呼和浩特</string>
		<string>惠东</string>
		<string>惠阳</string>
		<string>惠州</string>
		<string>葫芦岛</string>
		<string>湖州</string>
		<string>怀化</string>
	</array>
	<key>j</key>
	<array>
		<string>江都</string>
		<string>江门</string>
		<string>姜堰</string>
		<string>江阴</string>
		<string>焦作</string>
		<string>嘉兴</string>
		<string>揭阳</string>
		<string>吉林</string>
		<string>济南</string>
		<string>荆门</string>
		<string>荆沙</string>
		<string>金华</string>
		<string>济宁</string>
		<string>晋江</string>
		<string>晋中</string>
		<string>锦州</string>
		<string>九江</string>
	</array>
	<key>k</key>
	<array>
		<string>开封</string>
		<string>开平</string>
		<string>昆明</string>
		<string>昆山</string>
	</array>
	<key>l</key>
	<array>
		<string>莱芜</string>
		<string>莱西</string>
		<string>廊坊</string>
		<string>兰州</string>
		<string>连云港</string>
		<string>聊城</string>
		<string>列东</string>
		<string>醴陵</string>
		<string>临汾</string>
		<string>临沂</string>
		<string>丽水</string>
		<string>浏阳</string>
		<string>柳州</string>
		<string>龙岩</string>
		<string>六安</string>
		<string>陆丰</string>
		<string>漯河</string>
		<string>洛阳</string>
		<string>泸州</string>
	</array>
	<key>m</key>
	<array>
		<string>茂名</string>
		<string>梅州</string>
		<string>绵阳</string>
	</array>
	<key>n</key>
	<array>
		<string>南昌</string>
		<string>南充</string>
		<string>南京</string>
		<string>南宁</string>
		<string>南平</string>
		<string>南通</string>
		<string>南阳</string>
		<string>内江</string>
		<string>宁波</string>
	</array>
	<key>p</key>
	<array>
		<string>盘锦</string>
		<string>番禺</string>
		<string>平顶山</string>
		<string>莆田</string>
		<string>濮阳</string>
	</array>
	<key>q</key>
	<array>
		<string>启东</string>
		<string>青岛</string>
		<string>清远</string>
		<string>秦皇岛</string>
		<string>钦州</string>
		<string>琼海</string>
		<string>齐齐哈尔</string>
		<string>全国</string>
		<string>泉州</string>
		<string>衢州</string>
	</array>
	<key>r</key>
	<array>
		<string>日照</string>
	</array>
	<key>s</key>
	<array>
		<string>三河</string>
		<string>三亚</string>
		<string>上海</string>
		<string>上虞</string>
		<string>汕头</string>
		<string>汕尾</string>
		<string>韶关</string>
		<string>绍兴</string>
		<string>邵阳</string>
		<string>沈阳</string>
		<string>深圳</string>
		<string>石家庄</string>
		<string>石狮</string>
		<string>十堰</string>
		<string>寿光</string>
		<string>四会</string>
		<string>随州</string>
		<string>苏州</string>
	</array>
	<key>t</key>
	<array>
		<string>泰安</string>
		<string>台山</string>
		<string>太原</string>
		<string>泰州</string>
		<string>台州</string>
		<string>塘沽</string>
		<string>唐山</string>
		<string>天津</string>
		<string>铜陵</string>
	</array>
	<key>w</key>
	<array>
		<string>潍坊</string>
		<string>威海</string>
		<string>渭南</string>
		<string>温州</string>
		<string>武汉</string>
		<string>芜湖</string>
		<string>吴江</string>
		<string>乌鲁木齐</string>
		<string>无锡</string>
		<string>梧州</string>
	</array>
	<key>x</key>
	<array>
		<string>厦门</string>
		<string>西安</string>
		<string>襄樊</string>
		<string>香港</string>
		<string>湘潭</string>
		<string>咸宁</string>
		<string>咸阳</string>
		<string>兴宁</string>
		<string>邢台</string>
		<string>西宁</string>
		<string>新乡</string>
		<string>宣城</string>
		<string>许昌</string>
		<string>徐州</string>
	</array>
	<key>y</key>
	<array>
		<string>盐城</string>
		<string>阳江</string>
		<string>阳泉</string>
		<string>阳朔</string>
		<string>扬州</string>
		<string>烟台</string>
		<string>宜昌</string>
		<string>银川</string>
		<string>英德</string>
		<string>义务</string>
		<string>宜兴</string>
		<string>仪征</string>
		<string>永康</string>
		<string>岳阳</string>
		<string>云浮</string>
		<string>玉林</string>
		<string>玉溪</string>
		<string>余姚</string>
	</array>
	<key>z</key>
	<array>
		<string>湛江</string>
		<string>肇庆</string>
		<string>招远</string>
		<string>郑州</string>
		<string>镇江</string>
		<string>中山</string>
		<string>周口</string>
		<string>舟山</string>
		<string>珠海</string>
		<string>诸暨</string>
		<string>株洲</string>
		<string>淄博</string>
		<string>自贡</string>
	</array>
</dict>
</plist>