iphone - Best way to maintain column data when importing multiple formats -


i have app imports .csv file , turns each line core data object stored in database. csv files importing have 40-something columns , each column maps attribute of core data object.

when started project, there 1 csv format working with, wrote 40-something lines of un-elegant static code import file, such as:

... newentity.yearofconstruction      = [nsnumber numberwithint:[[currentrow objectatindex:35] integervalue]]; newentity.assessedvalue           = [nsnumber numberwithint:[[currentrow objectatindex:36] integervalue]]; newentity.squarefootageofproperty = [nsnumber numberwithint:[[currentrow objectatindex:37] integervalue]]; ... 

now problem i've run import other csv file formats ordered differently in original use case.

rather write switch(csvformattype) , add additional 40-line sets of code, elegant way store csv column-to-core data mapping arbitrary number csv file types , 1 set of code create core data objects?

thanks!

i'm working on similar right now. create format array has keys nsdictionary want created, index of these keys matches column in csv file.
have until now:

+ (nsarray *)arrayfromcsv:(nsstring *)csv format:(nsarray *)format ignoreheader:(bool)ignore {     nsarray *datarows = [csv componentsseparatedbystring:@"\n"];     nsarray *checkrow = [[datarows objectatindex:0] componentsseparatedbystring:@";"];     nsinteger expectedrowcomponents = [checkrow count];     if ([format count] > expectedrowcomponents) {             // more format keys components             return nil;     }     nsmutablearray *returnarray = [nsmutablearray array];     ( nsinteger = 0; < [datarows count]; i++ ) {             if (i == 0 && ignore)                     // ignore first line in csv, "header"                     continue;             nsstring *row = [datarows objectatindex:i];             nsarray *rowcomponents = [row componentsseparatedbystring:@";"];             if ( [rowcomponents count] != expectedrowcomponents )                 continue;             nsmutabledictionary *tmpdict = [nsmutabledictionary dictionary];             ( nsinteger j = 0; j < [format count]; j++ ) {                 if ( [format objectatindex:j] != [nsnull null] ) {                             [tmpdict setobject:[rowcomponents objectatindex:j] forkey:[format objectatindex:j]];                 }             }             [returnarray addobject:tmpdict];         }     nslog(@"%@", csv);     nslog(@"%@", returnarray);     return returnarray; } 

but first step , supports simple fileformat. cells ";" inside not supported.

if csv format this: firstname ; lastname ; zip ; age
, want import firstname, lastname , age create importformat

format = [nsarray arraywithobjects: @"firstname", @"lastname", [nsnull null], @"age", nil]; 

then you'll nsdictionary keys firstname, lastname, age. , if format changes rearrange array importformat.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -