从CSV文件数据创建自定义格式化字典数组 [英] Create a Custom Formatted Dictionary Array from CSV File of Data

查看:174
本文介绍了从CSV文件数据创建自定义格式化字典数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个iPhone应用程序,其中包含一个位置字典数组(lat,long,point)。我通过手动输入每个值来创建数组。

I've created an iPhone app that has a dictionary array of locations (lat,long,point). I created the array by manually entering each value.

myLocationArray = @[
                 @{
                   kStation : @"1",
                   kLatitude : @( 41.656467),
                   kLongitude : @(-81.277963)
                   },
                 @{
                   kStation : @"2",
                   kLatitude : @(41.657118),
                   kLongitude : @(-81.276545)
                   },
                 @{
                   kStation : @"3",
                   kLatitude : @(41.658493),
                   kLongitude : @(-81.273542)
                   },
                  ...

这个是好的,但现在我想通过从.CSV文件获取数据以编程方式创建此数组。我有一个.CSV文件(TestCSV.csv),看起来像这样。

This is good and works but now I want to create this array programmatically by getting the data from a .CSV file. I have a .CSV file (TestCSV.csv) that looks like this.

41.656467,-81.277963,27200
41.657118,-81.276545,27650
41.658493,-81.273542,28631.5
41.660728,-81.268547,30195
41.661830,-81.266065,30991
41.662828,-81.263819,31700
41.663677,-81.261962,32300
41.664578,-81.259909,32950
41.666210,-81.256312,34100
41.666921,-81.254708,34605
41.668043,-81.252191,35400
41.669044,-81.250043,36099
41.670120,-81.247495,36900
41.670778,-81.245957,37380
41.671459,-81.244292,37905
41.672028,-81.242832,38349
41.672487,-81.241702,38700
41.673106,-81.240175,39175
41.674364,-81.237007,40150
41.675170,-81.235038,40762.5
41.675716,-81.233698,41182
41.676143,-81.232614,41516

具体来说,我想创建myLocationArray(格式如图所示)直接从TestCSV.csv阅读。我不熟悉实现这一目标的代码,并且非常欣赏一些方向。

Specifically, I'd like to create myLocationArray (with formatting as shown) by reading straight from the TestCSV.csv. I'm not familiar with the code to achieve this and would really appreciate some direction.

另外,如果数据是通过文本文件而不是csv提供的,那么会有什么不同或更容易吗?

Also, would it be any different or easier if the data was provided via text file instead of csv?

推荐答案

尝试

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Locations" ofType:@"csv"];
NSString *csvString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];

NSArray *locations = [csvString componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

NSMutableArray *locationsArray = [NSMutableArray array];
for (NSString * location in locations)
{

    NSArray *components = [location componentsSeparatedByString:@","];

    double latitude   = [components[0] doubleValue];
    double longitude  = [components[1] doubleValue];
    NSString *station =  components[2];

    NSDictionary *dict = @{@"kLatitude": @(latitude),
                           @"kLongitude": @(longitude),
                           @"kStation": station};

    [locationsArray addObject:dict];

}

CSV文件

答案启发

这篇关于从CSV文件数据创建自定义格式化字典数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆