【发布时间】:2012-01-31 21:06:30
【问题描述】:
我有一个逗号分隔的文件需要解析。它看起来像这样:
... "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
#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 在第 16 行 (BAD_ACCESS) 崩溃,由于我是 4.2 调试新手,我不确定自己做错了什么。但是,日志显示“流”。我很确定我在[stream open] 上崩溃了。对后续步骤有何建议?
【问题讨论】: