14

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.

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

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

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

Guillaume
  • 21,685
  • 6
  • 63
  • 95
Jon
  • 4,732
  • 6
  • 44
  • 67

1 Answers1

33

I'm glad you like it :)

Basically, CHCSVParser only parses 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.

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

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.

So let's say your CSV file looks like this:

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.

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

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • Thanks, Dave, that helps a lot! So where is the best place to implement this method, into my table VC's `viewDidLoad` method? And the API automatically knows that when there is a `,` within two `""`, that that is all one string? – Jon Aug 06 '11 at 00:29
  • @Jon That was a typo that I fixed. Re: best place? Inside a VC is a decent place to put it, I suppose. The best place to put it is something that depends entirely on your app. :) – Dave DeLong Aug 06 '11 at 01:11
  • Thanks Dave. I tried doing it in ViewDidLoad but it takes forever for the app to open now. Do you have any other sugggestions? It works fine in simulator but on my device it takes like 30 sec for the app to open. I need this data to load the initial tableView. – Jon Aug 06 '11 at 01:53
  • @Jon it should work just fine on a background thread that then just dispatches the info back to the main thread when it's done. – Dave DeLong Aug 06 '11 at 01:55
  • Ok ill try that. I haven't worked with threads before so Ill have to read into that. Right now it just ends up crashing because of watchdog I think (apple kills it). – Jon Aug 06 '11 at 01:57
  • So I put the parse on the background thread and its still taking about 40 seconds to parse through all the data. What other options do I have? Because this data is supposed to load the tableview for the initial view that the user sees when launching the app. – Jon Aug 06 '11 at 10:33
  • @Jon Contact me via my website and I'll take a look at the performance characteristics. – Dave DeLong Aug 06 '11 at 15:41
  • @DaveDeLong When use the parser with your example above that has quotes ("Chicxulub,...Crater"), I get a string like this back "\"Chicxulub,...Crater\"" with the quotes in it still. Is that intended, or is there an option to remove the quotes? Thanks – Collin Aug 22 '13 at 16:10
  • @Collin You're looking for the sanitization option on the parser. – Dave DeLong Aug 22 '13 at 17:02