ios之oc省市区小地图

一 OC小地图之通过省市来输出区域


<span style="font-size:14px;">    NSString *filePath  = @"/Users/dlios/Desktop/666666/20150302demo01/20150302demo01/area.txt";</span>
//把文件装入缓冲区
<span style="font-size:14px;">    NSString *buffer = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
    <span style="color:#009900;">//把字符串拆分成数组</span>
    NSArray *array = [buffer componentsSeparatedByString:@"\n"];</span>
<span style="font-size:14px;">    </span><span style="font-size:12px;color:#009900;">//创建省份数组</span><span style="font-size:14px;">
    NSMutableArray *provinceList = [NSMutableArray array];
    NSCharacterSet *cSet = [NSCharacterSet characterSetWithCharactersInString:@" 0123456789"];

    for (NSString *s in array) {</span>
<span style="font-size:14px;"><span style="white-space:pre">	</span><span style="color:#009900;">//找到省份</span></span>
<span style="font-size:14px;">        if (![s hasPrefix:@" "]) {</span>
<span style="font-size:14px;"><span style="color:#009900;">            //省名称(去掉两边的空格和数字)</span>
            NSString *provinceName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//创建城市数组</span>
            NSMutableArray *cityList = [NSMutableArray array];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//创建省字典</span>
            NSDictionary *pDic = [NSDictionary dictionaryWithObjectsAndKeys:provinceName, @"name", cityList, @"list", nil];</span>
<span style="font-size:14px;">            <span style="color:#009900;background-color: rgb(255, 255, 255);">//装入省字典</span>
            [provinceList addObject:pDic];
        }</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到城市</span>
        if ([s hasPrefix:@"  "] && ![s hasPrefix:@"   "]) {</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到省字典</span>
            NSDictionary *pDic = [provinceList lastObject];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到城市数组</span>
            NSMutableArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;">            /<span style="color:#009900;">/城市名称(去掉两边的空格和数字)</span>
            NSString *cityName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//创建区数组</span>
            NSMutableArray *districtList = [NSMutableArray array];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//创建城市字典</span>
            NSDictionary *cDic = [NSDictionary dictionaryWithObjectsAndKeys:cityName, @"name", districtList, @"list", nil];       </span>
<span style="font-size:14px;"><span style="white-space:pre">	</span>    <span style="color:#009900;">//装入城市字典</span>
            [cityList addObject:cDic];
        }</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到区</span>
        if ([s hasPrefix:@"   "]) {</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到省字典</span>
            NSDictionary *pDic = [provinceList lastObject];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到城市数组</span>
            NSArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到城市字典</span>
            NSDictionary *cDic = [cityList lastObject];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//找到区数组</span>
            NSMutableArray *districtList = [cDic objectForKey:@"list"];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//区名称(去掉两边的空格和数字)</span>
            NSString *dName = [s stringByTrimmingCharactersInSet:cSet];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//装入区数组</span>
            [districtList addObject:dName];
            
        }
    }</span>
<span style="font-size:14px;">            <span style="color:#009900;">//取出省字典</span>
    for (NSDictionary *pDic in provinceList) {</span>
<span style="font-size:14px;"><span style="white-space:pre">	</span>    <span style="color:#009900;">//省名称</span>
        NSString *provinceName = [pDic objectForKey:@"name"];</span>
<span style="font-size:14px;"> <span style="white-space:pre">	</span>    <span style="color:#009900;">//判断省名称</span>
        if ([provinceName hasPrefix:@"山东"]) {</span>
<span style="font-size:14px;">            <span style="color:#009900;">//创建城市数组</span>
            NSMutableArray *cityList = [pDic objectForKey:@"list"];</span>
<span style="font-size:14px;">            <span style="color:#009900;">//取出城市字典</span>
            for (NSDictionary *cDic in cityList) {</span>
<span style="font-size:14px;">              <span style="color:#009900;">//城市名称</span>
                NSString *cityName = [cDic objectForKey:@"name"];</span>
<span style="font-size:14px;">                <span style="color:#009900;">//判断市</span>
                if ([cityName hasPrefix:@"德州"]) {</span>
<span style="font-size:14px;">                    <span style="color:#009900;">//创建区数组</span>
                    NSMutableArray *districtList = [cDic objectForKey:@"list"];</span>
<span style="font-size:14px;">                    <span style="color:#009900;">//取出区名称</span>
                    for (NSString *dName in districtList) {
                        NSLog(@"%@", dName);
                    }
                }
            }
        }
    }</span>



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值