The CSV analyzer I wrote ( http://github.com/davedelong/CHCSVParser ) can parse CSV files into NSArray from NSArrays (if you use the NSArray category included in the code), and I just added a method to the category to take one of these arrays and write it back to the CSV file.
So, if you have an NSArray of NSArrays , you can write it to a CSV file, for example:
[myArray writeToCSVFile:myCSVFile atomically:YES]
If it fails gracefully, unless you pass NSArray from NSArrays.
edit (caused by @Aran comment on the question), any object inside the subarrays will have its own -description method written to the CSV file. If you want to write a series of objects to a file, with various properties as fields of a CSV string, then my shell requires the properties to be placed in an array, and then this array will make up a string.
edit # 2 I just updated my CSV parser to reorganize the entry into my own class. Now you can do the following:
CHCSVWriter * csvWriter = [[CHCSVWriter alloc] initWithCSVFile:outputFile atomic:NO]; NSInteger numberOfColumns = 3; for (NSInteger currentIndex = 0; currentIndex < [csvArray count]; currentIndex++) { id field = [csvArray objectAtIndex:currentIndex]; [csvWriter writeField:field]; if ((currentIndex % numberOfColumns) == (numberOfColumns - 1)) { [csvWriter writeLine]; } } [csvWriter release];
This will write your csvArray to a file whose path is outputFile , and the file will look like this:
First Name,Last Name,Phone Tom,Chan,123 Peter,Wong,456 Mary's,"Cho""w",789
Dave delong
source share