我如何解析csv文件?

时间:2014-02-27 20:30:25

标签: ios csv

我试图实施一个标签,向我显示当前位置的天气数据,如下所示:

NSString *request = [NSString stringWithFormat:@"http://api.worldweatheronline.com/free/v1/weather.ashx?q=%@&format=csv&num_of_days=0&show_comments=no&key=myKeyThatIRemovedForThisQuestion",city];
    NSURL *URL = [NSURL URLWithString:request];
    NSError *error;
    NSString *csv = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];
    NSArray *items = [csv componentsSeparatedByString:@","];
    NSLog(csv);
    NSLog([items firstObject]);
    NSLog([items objectAtIndex:1]);

csv的日志记录行有效。 数组中第一个对象的日志记录行有效。

但objectatindex行引发意外错误:

  

libc ++ abi.dylib:以未捕获的类型异常终止   NSException

控制台上的csv示例:

2014-02-27 21:27:43.626  Clock[1470:70b] 08:28 PM,17,116,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png,Partly Cloudy ,9,15,100,E,0.2,59,16,1012,75
2014-02-27,18,64,8,46,13,22,169,SSE,263,http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png,Patchy light drizzle,3.8

如何让这个东西工作,这样我才能从数组中获取数据?

1 个答案:

答案 0 :(得分:0)

我几次使用Dave Delong的CSV解析器没有问题。这可以在这里找到:https://github.com/davedelong/CHCSVParser

它也可以通过CocoaPods作为CHCSVParser获得。这个库有一个测试套件 - 并且工作效率很高。

相关问题