Saving a CSV File Using CHCSVWriter

I am trying to write a large CSV file from the main managed data to the application document directory using CHCSVWriter.

While it looks fine, the cvs file does not seem to exist after completion.

NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filename = @"productFileExport.csv"; NSURL *fileURL = [NSURL fileURLWithPathComponents:[NSArray arrayWithObjects:documentsDirectory, filename, nil]]; CHCSVWriter *cvsExport = [[CHCSVWriter alloc] initWithCSVFile:[fileURL absoluteString] atomic:NO]; for (Product *prod in fetchedObjects) { [cvsExport writeLineOfFields:prod.code, prod.barcode, prod.name, prod.size, prod.casesize, nil]; } [cvsExport closeFile]; 

I believe this is a problem with the file name in which I tried to change initWithCVSFile: from [fileURL absoluteString] to [fileURL path] and [fileURL description] with no luck. Also, if I changed atomic to YES, it will get the following error:

* Application termination due to an uncaught exception 'NSInvalidArgumentException', reason: '* - [NSFileManager moveItemAtPath: toPath: error:]: destination path is nil'

Any help would be greatly appreciated, thanks

+4
source share
1 answer

The problem was in cvsExport writeLineOfFields , the first object passed was zero, so nothing was written to the file.

you must also pass initWithCVSFile: [fileURL path] .

+2
source

All Articles