如何将 txt 文件解析为 NSDictionary?
我的文件看起来像
JACK - (mr) - address
99000 - City
Phone : 09 93 81 48 51
Website
这是我的代码:
NSString *file = [[NSBundle mainBundle]pathForResource:@"MyFile" ofType:@"txt"];
NSError *error;
NSString *text = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:&error];
NSArray * testArray = [[NSArray alloc] init];
testArray = [text componentsSeparatedByString:@" \n"];
之后
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *s in testArray)
{
NSArray *arr = [s componentsSeparatedByString:@" -"];
[dict setObject:[arr objectAtIndex:0] forKey:@"name"];
NSLog(@"Dictionary: %@", [dict description]);
}
我的 NSLog 给了我:
Dictionary: {
name = "JACK";
}
如何解析文件的其余部分以获取“mr”并在地址之后......?