lps
lps

Reputation: 227

Trying to write coredata to csv using chcsvparser

I've done the following to put a fetched request into an array of arrays but now i don't know which methods i need to call from chcsvparser to write this into a csv file

NSArray *objectsForExport = [fetchedResultsController fetchedObjects];
NSArray *exportKeys = [NSArray arrayWithObjects:@"best_checkout", @"darts_thrown", @"high_score", @"score_100", @"score_140", @"score_180",@"three_dart_average",nil];

NSMutableArray *csvObjects = [NSMutableArray arrayWithCapacity:[objectsForExport count]];
for (NSManagedObject *object in objectsForExport) {
    NSMutableArray *anObjectArray = [NSMutableArray arrayWithCapacity:[exportKeys count]];
    for (NSString *key in exportKeys) {
        id value = [object valueForKey:key];
        if (!value) {
            value = @"";
        }
        [anObjectArray addObject:[value description]];
    }
    [csvObjects addObject:anObjectArray];
}

Upvotes: 1

Views: 1725

Answers (2)

paulbailey
paulbailey

Reputation: 5346

As Johann suggests, you should use the writeToCSVFile:atomically: convenience method. However, be aware that using it as you describe in your comment is not correct.

The NSString you pass in should be the filepath you want the data writing to.

Upvotes: 1

Johann Dirdal
Johann Dirdal

Reputation: 1090

This webpage should give you the necessary information and methods when writing CSV files:

https://github.com/davedelong/CHCSVParser#readme

Hope this helps!

Upvotes: 0

Related Questions