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?

3
  • csv is a dirty file format, so you have a dirty code to parse. But can you change your data file from csv to json? Commented Jan 2, 2012 at 14:54
  • csv is pretty horrible... There are good reasons for using it in this case though. Commented Jan 2, 2012 at 14:56
  • so there is only the dirty way... Commented Jan 2, 2012 at 14:57

4 Answers 4

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 :)

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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?)

1 Comment

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.
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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.