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]
。有关下一步的任何建议?
我补充说(对我的代码和我的编辑),但我仍然得到相同的控制台以及相同的崩溃。 – Jacksonkr 2012-01-31 21:12:21
你能从控制台粘贴确切的输出吗? – jrtc27 2012-01-31 21:46:49
你也必须调用'dict = [[NSMutableDictionary alloc] init]'。 – 2012-01-31 23:52:50