.m
#import <Foundation/Foundation.h>
#import "Node.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSString *path = @"/Users/dllo/Desktop/NaPolun/Provide/Provide/area.txt";
NSString *file = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSArray *arr = [file componentsSeparatedByString:@"\n"];
Node *rootNode = [Node nodeWithName:@"china"];
for (NSString *tmpStr in arr) {
Node *node = [Node nodeWithName:tmpStr];
if (![tmpStr hasPrefix:@" "]) {
[rootNode addNode:node];
} else if ([tmpStr hasPrefix:@" "]) {
Node *proNode = [rootNode lastNode];
[proNode addNode:node];
} else {
Node *proNode = [rootNode lastNode];
Node *cityNode = [proNode lastNode];
[cityNode addNode:node];
}
}
[rootNode sayHi];
}
return 0;
}
Node.h
#import <Foundation/Foundation.h>
@interface Node : NSObject
@property (nonatomic, retain) NSString *name;
+ (instancetype)nodeWithName:(NSString *)name;
- (void)sayHi;
- (void)addNode:(Node *)node;
- (Node *)lastNode;
@end
Node.m
#import "Node.h"
@interface Node()
@property (nonatomic, retain) NSMutableArray *areaArr;
@end
@implementation Node
+ (instancetype)nodeWithName:(NSString *)name
{
Node *node = [[Node alloc] init];
node.name =name;
node.areaArr = [NSMutableArray array];
return [node autorelease];
}
- (void)sayHi
{
for (Node *subNode in self.areaArr) {
NSLog(@"%@", subNode.name);
[subNode sayHi];
}
}
-(void)addNode:(Node *)node
{
[self.areaArr addObject:node];
}
- (Node *)lastNode
{
return [self.areaArr lastObject];
}
- (void)dealloc
{
[_name release];
[_areaArr release];
[super dealloc];
}
@end