xcode 使用 CHCSVParser 的基础介绍
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6961367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Basics Introduction To Using CHCSVParser
提问by Jon
I'm implementing CHCSVParserinto 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.
我正在将CHCSVParser实施到我的 iPhone 应用程序中(感谢 Dave!)但是我真的很困惑如何使用它。我已经阅读了自述文件并搜索了一些关于 SO 的问题,但仍然不是 100% 确定该怎么做。
I have a .CSV file with maybe 5000 rows of data and 3-4 columns.
我有一个 .CSV 文件,其中可能有 5000 行数据和 3-4 列。
I want this data to in return, load my UITableView along with its corresponding detailViewController.
我希望这些数据作为回报,加载我的 UITableView 及其相应的 detailViewController。
So I'm assuming I need to somehow implement the API's array method but can anyone help get me started?
所以我假设我需要以某种方式实现 API 的数组方法,但有人可以帮助我入门吗?
回答by Dave DeLong
I'm glad you like it :)
我很高兴你喜欢它 :)
Basically, CHCSVParser
onlyparses CSV files. You give it a path to a CSV file, and it'll give you back a whole bunch of NSStrings
. What you do beyond that point is entirely up to you.
基本上,CHCSVParser
只解析 CSV 文件。你给它一个 CSV 文件的路径,它会给你一大堆NSStrings
. 在那之后你做什么完全取决于你。
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:
假设您在 iOS 应用程序中包含了一个名为“Data.csv”的 CSV 文件。以下是您CHCSVParser
用来解析它的方法:
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;
}
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 thatarray is an NSString
.
此时,rows
是一个数组。中的每个元素rows
本身都是一个数组,表示 CSV 文件中的一行。该数组的每个元素都是一个NSString
.
So let's say your CSV file looks like this:
假设您的 CSV 文件如下所示:
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];
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.
你用它做什么就是你的事。CSV 解析器对 一无所知UITableView
,因此您可以获取这些数据并以您可以轻松处理并适合您的数据模型的方式重新构建它。
Also, remember that by using CHCSVParser
, you agree to abide the terms under which it is made available. :)
另外,请记住,使用CHCSVParser
,即表示您同意遵守提供它的条款。:)