省市县用对象实现

//

//  main.m

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/3.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>

#import "Operation.h"

#import "City.h"

#import "provice.h"


int main(int argc, const char * argv[]) {

    @autoreleasepool {

        Operation *oper = [[Operation alloc] init];

        [oper operation];

        [oper info];

    }

    return 0;

}

//

//  Operation.h

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "City.h"

@class provice;

@class City;

@interface Operation : City

@property (nonatomic, retain)NSMutableArray *proArr;

@property (nonatomic, retain)NSArray *arr;

- (id)init;

- (void)operation;

- (void)info;

@end


//

//  Operation.m

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "Operation.h"

@class provice;

@class City;

@implementation Operation

- (id)init

{

    NSString *filePath = @"/Users/dllo/Desktop/省市区/area.txt";

    NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

   // NSMutableArray *arr = [NSMutableArray array];

    NSArray *arr  = [str componentsSeparatedByString:@"\n"];

    NSMutableArray *proArr = [NSMutableArray array];

    self = [super init];

    if (self) {

        [self setProArr:proArr];

        [self setArr:arr];

    }

    return self;

}

- (void)operation

{

    for (NSString *temp in self.arr) {

        if (![temp hasPrefix:@" "]) {

            provice *pro = [[provice alloc]init];

            [pro setProName:temp];

            [self.proArr addObject:pro];

        } else if([temp hasPrefix:@"  "] && ![temp hasPrefix:@"   "]) {

            City *city = [[City alloc]init];

            [[[self.proArr lastObject] cityArr]addObject:city];

            [[[[self.proArr lastObject] cityArr]lastObject]setCityName:temp];

            

        } else {

            [[[[[self.proArr lastObject] cityArr ]lastObject] quArr]addObject:temp];

        }

    }

}

- (void)info

{

    for (provice *pro in self.proArr) {

        NSLog(@"%@", [pro proName]);

        for (City *city in pro.cityArr) {

            NSLog(@"%@", [city cityName]);

            for (NSString *temp in city.quArr) {

                NSLog(@"%@", temp);

            }

        }

    }

}

@end


//

//  City.h

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "provice.h"


@interface City : provice


@property (nonatomic, retain)NSMutableArray *quArr;

@property (nonatomic, copy)NSString *cityName;

- (id)init;

@end


//

//  City.m

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "City.h"


@implementation City

- (id)init

{

    NSMutableArray *quArr = [NSMutableArray array];

    NSString *cityName = [[NSString alloc]init];

    self = [super init];

    if (self) {

        [self setQuArr:quArr];

        [self setCityName:cityName];

    }

    return self;

}

@end


//

//  provice.h

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import <Foundation/Foundation.h>


@interface provice : NSObject

@property (nonatomic, retain)NSMutableArray *cityArr;

@property (nonatomic, copy)NSString *proName;

- (id)init;

@end


//

//  provice.m

//  练习省市区用对象实现

//

//  Created by dllo on 15/11/4.

//  Copyright (c) 2015 dllo. All rights reserved.

//


#import "provice.h"


@implementation provice

- (id)init

{

    NSMutableArray *cityArr = [NSMutableArray array];

    NSString *proName = [[NSString alloc]init];

    self = [super init];

    if (self) {

        [self setProName:proName];

        [self setCityArr:cityArr];

        

    }

    return self;

}

@end



posted @ 2015-11-08 14:21  挽月细数风流  阅读(180)  评论(0编辑  收藏  举报