NeilD
NeilD

Reputation: 2298

Objective-C: Parse string to object

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?

Upvotes: 0

Views: 537

Answers (4)

jlehr
jlehr

Reputation: 15617

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

Upvotes: 0

Mike Fahy
Mike Fahy

Reputation: 5707

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

Upvotes: 0

Ignacio Inglese
Ignacio Inglese

Reputation: 2605

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.

Upvotes: 0

Thomas Johan Eggum
Thomas Johan Eggum

Reputation: 915

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

Upvotes: 2

Related Questions