0

I have a simple object. It has several NSString properties (propertyA, propertyB, propertyC).

I have a string (read from a csv file) in the following form:

this is value A, this is value B, this is value C
another row A, another row B

Notice that the second row is missing the last property.

I want to parse the string into my object. Currently I'm grabbing a line from the csv file and then doing this:

MyObject *something = [[MyObject alloc] init];
NSArray *split = [line componentsSeparatedByString:@","];

if (something.count > 0)
    something.propertyA = [split objectAtIndex:0];

if (something.count > 1) 
    something.propertyB = [split objectAtIndex:1];

if (something.count > 2)
    something.propertyC = [split objectAtIndex:2];

This works well, but feels really horrible and hacky! Has anyone got any suggestions for how I can improve the code?

NeilD
  • 2,278
  • 3
  • 24
  • 28

4 Answers4

2

Take a look at this tread about parsing CSV Where can I find a CSV to NSArray parser for Objective-C?

Dave DeLong wrote a CSV-parser library, you can find it here: https://github.com/davedelong/CHCSVParser

Hope this helps :)

Community
  • 1
  • 1
0

Here's a CSV parsing extension to NSString that I used in the past to handle CSV data.

http://www.macresearch.org/cocoa-scientists-part-xxvi-parsing-csv-data

If basically adds a -(NSArray *)csvRows method to NSString that returns a NSArray with each row on your data and a NSArray inside each row to handle the columns. It's the simplest and cleanest way I found so far to deal with the ocasional CSV data that comes up.

Ignacio Inglese
  • 2,605
  • 16
  • 18
0

Your approach actually seems pretty sound, given the input format of the file, and assuming that no individual items actually contain a comma within themselves. As others have mentioned, CSV and/or custom flat files require custom solutions to get what you want from them.

If the approach above gets you the data you want, then I say use it. If it doesn't though, perhaps you can share the actual problem you're experiencing (ie, what data are you getting out, and what were you expecting?)

Mike Fahy
  • 5,487
  • 4
  • 24
  • 28
  • Ha. Understood. Sometimes dirty data is just dirty, though. You COULD use a CSV-parsing library, but if you're confident the data will remain in the same format you currently have (a big "if," of course), I'm not sure you'll get much from it (other than an extra file to maintain). My 2 cents. – Mike Fahy Jan 02 '12 at 15:51
0

Consider using an array of keys that correspond to MyObject property names. For example:

NSString *propertyNames[] = { @"property1", @"property2", @"property3" };

NSArray *values = [line componentsSeparatedByString:@","];
NSArray *keys = [NSArray arrayWithObjects:propertyNames count:[values count]];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:values forKeys:keys];

MyObject obj = [[MyObject alloc] init];
[obj setValuesForKeysWithDictionary:dict];

You could then consider adding an initWithDictionary: method to MyObject that calls setValuesForKeysWithDictionary. That would help streamline things a little further, allowing you to write the last two lines above as a single line:

MyObject obj = [[MyObject alloc] initWithDictionary:dict];
jlehr
  • 15,557
  • 5
  • 43
  • 45