-3

我创建了一个 iPhone 应用程序,它有一个位置字典数组(纬度、经度、点)。我通过手动输入每个值来创建数组。

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)。

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

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

此外,如果数据是通过文本文件而不是 csv 提供的,是否会有所不同或更容易?

4

3 回答 3

5

尝试

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 文件

答案灵感来自

于 2013-05-11T16:48:18.477 回答
1

Anupdas提供的答案。干杯!

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];

}

于 2013-05-14T01:06:51.813 回答
1

csv 文件是纯文本文件。要实现您想要的,您可以使用NSScannerNSString componentsSeparatedByString:其他一切都非常简单。

于 2013-05-11T16:21:18.980 回答