我有一个CSV文件,其数据结构如下:
40.697776,-73.973605,“1 CARLTON AVENUE,BROOKLYN,NEW YORK,”
40.812784,-73.952996,“1 CONVENT AVENUE,MANHATTAN,NEW YORK,”
我想将每一行转换为NSDictionary,其中NSArray包含所有字典。我希望关键是纬度,经度,地址。
我是目标c的新手,所以我有一些问题可以解决这个问题。任何帮助将不胜感激。
答案 0 :(得分:4)
没有什么可以自动完成的。您需要使用现有的csv parser或编写自己的{{3}}。从空NSMutableArray
开始,然后遍历CSV文件中的每一行。对于每一行,以NSMutableDictionary
开头,一次添加一个值。完成该行后,将NSMutableDictionary
添加到NSMutableArray
的末尾。代码看起来如下所示,但它实际上取决于您使用的CSV解析器:
NSMutableArray* allRows = [NSMutableArray array];
//how this loop is actually done depends on your CSV parser
//if it's a SAX style parser, then it won't even be a loop, it will be a callback
for(every row in the CSV file) {
//how you get these three fields also depends on your CSV parser
NSNumber* firstField = ?;
NSNumber* secondField = ?;
NSString* thirdField = ?;
NSMutableDictionary* row = [NSMutableDictionary dictionary];
[row setObject:firstField forKey:@"latitude"];
[row setObject:secondField forKey:@"longitude"];
[row setObject:thirdField forKey:@"address"];
[allRows addObject:row];
}