基本介绍使用CHCSVParser [英] Basics Introduction To Using CHCSVParser

查看:283
本文介绍了基本介绍使用CHCSVParser的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的iPhone应用程式中执行 CHCSVParser (感谢Dave!),但我真的很困惑如何使用它。我已阅读read-me并搜索了一些关于SO的问题,但仍然不能100%确定该怎么做。

I'm implementing CHCSVParser into my iPhone app (thanks Dave!) however I'm really confused on how to use it. I've read the read-me and searched some questions on SO but still not 100% sure what to do.

我有一个.CSV文件,可能有5000行数据和3-4列。

I have a .CSV file with maybe 5000 rows of data and 3-4 columns.

我想要这个数据返回,加载我的UITableView及其相应的detailViewController。

I want this data to in return, load my UITableView along with its corresponding detailViewController.

所以我假设我需要以某种方式实现API的数组方法,但任何人都可以帮助我开始。

So I'm assuming I need to somehow implement the API's array method but can anyone help get me started?

推荐答案

我很高兴你喜欢它)

基本上, CHCSVParser CSV文件。你给它一个CSV文件的路径,它会给你一堆 NSStrings

因此,假设您在iOS应用程式中加入了名为Data.csv的CSV档案。下面是如何使用 CHCSVParser 来解析它:

So let's say you've included a CSV file in your iOS app called "Data.csv". Here's how you'd use CHCSVParser to parse it:

NSString *path = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"csv"];

NSError *error = nil;
NSArray *rows = [NSArray arrayWithContentsOfCSVFile:path encoding:NSUTF8StringEncoding error:&error];
if (rows == nil) {
  //something went wrong; log the error and exit
  NSLog(@"error parsing file: %@", error);
  return;
}

此时, rows 是一个数组。 rows 中的每个元素本身是一个表示CSV文件中单个行的数组。 数组的每个元素都是 NSString

At this point, rows is an array. Each element in rows is itself an array representing a single row in the CSV file. And each element of that array is an NSString.

文件如下:

Barringer,Arizona,United States,Earth
"Chicxulub, Extinction Event Crater",,Mexico,Earth
Tycho,,,Moon
Lonar,Maharashtra,India,Earth

如果你通过解析器运行它,你将得到相当于:

If you run it through the parser, you'll get back the equivalent of this:

[NSArray arrayWithObjects:
 [NSArray arrayWithObjects:@"Barringer",@"Arizona",@"United States",@"Earth",nil],
 [NSArray arrayWithObjects:@"Chicxulub, Extinction Event Crater",@"",@"Mexico",@"Earth",nil],
 [NSArray arrayWithObjects:@"Tycho",@"",@"",@"Moon",nil],
 [NSArray arrayWithObjects:@"Lonar",@"Maharashtra",@"India",@"Earth",nil],
 nil];

那么你做什么就是你的事。 CSV解析器不知道有关 UITableView 的任何事情,所以你得到这些数据,并重新构建它,你可以很容易处理,

What you do with it then is your business. The CSV parser doesn't know anything about UITableView, so you get to take this data and re-structure it in a way that you're comfortable dealing with and that fits in to your data model.

此外,请记住,通过使用 CHCSVParser ,您同意遵守相关条款。 :)

Also, remember that by using CHCSVParser, you agree to abide the terms under which it is made available. :)

这篇关于基本介绍使用CHCSVParser的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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