2012-01-31 76 views
0

我有一个需要解析的逗号分隔文件。它看起来像这样:ios解析文本文件

... "John", "Smith", "New York", "10038" ...

我处理这些记录每个文件约1000个。我的计划是将它们解析成dictionary

从我迄今为止发现的,我应该使用NSInputStream。我当前的代码如下所示:

firstViewController.m

... 
- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    GruParser *stream = [[GruParser alloc] init]; 
    [stream parse:[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"gru"]]; 
} 
... 

GruParser.m 16行(BAD_ACCESS)

#import "GruParser.h" 

@implementation GruParser 

-(id)init { 
    if(self = [super init]) { 
     dict = [NSMutableDictionary alloc]; 
    } 
    return self; 
} 

-(void)parse:(NSString *)path { 
    NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:path]; 
    //NSInputStream *stream = [NSInputStream inputStreamWithURL:[NSURL URLWithString:@"http://www.w3schools.com/xml/note.xml"]]; 

    [stream setDelegate:self]; 
    [stream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
    [stream open]; 
    NSLog(@"%@", @"parse"); 
} 

-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode { 
    NSLog(@"%@", @"stream event"); 
} 

@end 

main.m崩溃,因为我是4.2的调试小白我我不知道我做错了什么。但是,日志说“流”。我很确定我在碰撞[stream open]。有关下一步的任何建议?

回答

0

您需要使用GruParser *stream = [[GruParser alloc] init];而不是GruParser *stream = [GruParser alloc]; - 您将创建一个未初始化的对象,从而导致崩溃。

+0

我补充说(对我的代码和我的编辑),但我仍然得到相同的控制台以及相同的崩溃。 – Jacksonkr 2012-01-31 21:12:21

+0

你能从控制台粘贴确切的输出吗? – jrtc27 2012-01-31 21:46:49

+0

你也必须调用'dict = [[NSMutableDictionary alloc] init]'。 – 2012-01-31 23:52:50