Objective-C省市区用类代替字典存储和读取

来源:互联网 发布:淘宝网羊绒面料批发 编辑:程序博客网 时间:2024/05/15 23:51

文档请到http://wenku.baidu.com/view/234bb67b52ea551810a687ce下载

创建Country,City,District类
main.m
#import <Foundation/Foundation.h>#import "Country.h"#import "Province.h"#import "City.h"#import "District.h"int main(int argc, const char * argv[]) {    NSString * file = @"/Users/henry/Desktop/OC 整理/字符串常用api/2.25/area副本.txt";    NSString * buffer = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];    NSArray * array = [buffer componentsSeparatedByString:@"\n"];    //创建要删除的字符集合    NSCharacterSet * cSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789 "];        Country * country = [Country countryWithName:@"China"];    //遍历文件每个元素    for (NSString * str in array) {        //找到省        if (![str hasPrefix:@"  "]) {            NSString * s1 = [str stringByTrimmingCharactersInSet:cSet];            Province * province = [Province ProvinceWithName:s1];            [country.province addObject:province];        }        //找到市        if ([str hasPrefix:@"  "] && ![str hasPrefix:@"    "]) {            NSString * s2 = [str stringByTrimmingCharactersInSet:cSet];            Province * province = [country.province lastObject];            City * city = [City cityWithName:s2];            [province.city addObject:city];        }        //找到区        if ([str hasPrefix:@"    "]) {            NSString * s3 = [str stringByTrimmingCharactersInSet:cSet];            Province * province = [country.province lastObject];            City * city = [province.city lastObject];            District * district = [District districtWithName:s3];            [city.district addObject:district];        }    }    //输出    for (Province * province in country.province) {        if ([province.name isEqualToString:@"北京"]) {            for (City * city in province.city) {                if ([city.name isEqualToString:@"北京市"]) {                    for (District * district in city.district) {                        NSLog(@"%@", district.name);                    }                }            }        }    }    return 0;}
Country.h
#import <Foundation/Foundation.h>@interface Country : NSObject@property(nonatomic, retain)NSString * name;@property(nonatomic, retain)NSMutableArray * province;- (id)initWithName:(NSString *)name;+ (id)countryWithName:(NSString *)name;- (void)dealloc;@end
Country.m
#import "Country.h"@implementation Country- (id)initWithName:(NSString *)name {    self = [super init];    if (self) {        [self setName:name];        [self setProvince:[NSMutableArray array]];    }    return self;}+ (id)countryWithName:(NSString *)name {    Country * country = [[Country alloc] initWithName:name];    return [country autorelease];}- (void)dealloc {    [_name release];    [_province release];    [super dealloc];}@end
Province.h
#import <Foundation/Foundation.h>@interface Province : NSObject@property(nonatomic, retain)NSString * name;@property(nonatomic, retain)NSMutableArray * city;- (id)initWithName:(NSString *)name;+ (id)ProvinceWithName:(NSString *)name;- (void)dealloc;@end
Province.m
#import "Province.h"@implementation Province- (id)initWithName:(NSString *)name {    self = [super init];    if (self) {        [self setName:name];        [self setCity:[NSMutableArray array]];    }    return self;}+ (id)ProvinceWithName:(NSString *)name {    Province * province = [[Province alloc] initWithName:name];    return [province autorelease];}- (void)dealloc {    [_name release];    [_city release];    [super dealloc];}@end
City.h
#import <Foundation/Foundation.h>@interface City : NSObject@property(nonatomic, retain)NSString * name;@property(nonatomic, retain)NSMutableArray * district;- (id)initWithName:(NSString *)name;+ (id)cityWithName:(NSString *)name;- (void)dealloc;@end
City.m
#import "City.h"@implementation City- (id)initWithName:(NSString *)name {    self = [super init];    if (self) {        [self setName:name];        [self setDistrict:[NSMutableArray array]];    }    return self;}+ (id)cityWithName:(NSString *)name {    City * city = [[City alloc] initWithName:name];    return [city autorelease];}- (void)dealloc {    [_name release];    [_district release];    [super dealloc];}@end
District.h
#import <Foundation/Foundation.h>@interface District : NSObject@property(nonatomic, retain)NSString * name;- (id)initWithName:(NSString *)name;+ (id)districtWithName:(NSString *)name;- (void)dealloc;@end
District.m
#import "District.h"@implementation District- (id)initWithName:(NSString *)name {    self = [super init];    if (self) {        [self setName:name];    }    return self;}+ (id)districtWithName:(NSString *)name {    District * district = [[District alloc] initWithName:name];    return [district autorelease];}- (void)dealloc {    [_name release];    [super dealloc];}@end
<span style="color:#000099;">边栏截图</span>
<span style="color:#000099;"><img src="http://img.blog.csdn.net/20150227194623865?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1eW9uZ3Blbmdh/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span>
0 0
原创粉丝点击