views:

83

answers:

2

Hi, everyone,

I am writing an iPhone application, which contains a function. It can convert the NSMutableArray to a CSV file. However, I don't know how to do. Can anyone help me to do this? Thank you very much.

// ------ Update -----

Thank you for everyone reply.

Actually, the array contains the objects of the elements, but I can convent it all to the array like the following (I guess it is more easy to do this).

The array is NSMutableArray *csvArray and the array contains data, like the following example.

csvArray[0] = First Name
csvArray[1] = Last Name
csvArray[2] = Phone
csvArray[3] = Tom
csvArray[4] = Chan
csvArray[5] = 123
csvArray[6] = Peter
csvArray[7] = Wong
csvArray[8] = 456
csvArray[9] = Mary's
csvArray[10] = Cho"w
csvArray[11] = 789...

There are 3 tabs in the begin of the array, which is first name, last name and the phone. For the data, it also contains the " and , symbol. Therefore, I cannot just cut the array by the ',' symbol.

The format I would like to output is the following

//---------------------------

First Name, Last Name, Phone  // <- it have the \r\n between each row of data
Tom, Chan, 123
Peter, Wong, 456
Mary's, Cho"w, 789 ...

//---------------------------
+2  A: 
NSString *csv = [myArray componentsJoinedByString: @","];

But be warned, this will not account for commas within the array's elements. You will need to define some escaping/unescaping technique for that, and that's entirely up to you to do.

Jonathan Grynspan
This won't work terribly well unless the array is populated with strings (in which case it only produces a single-row CSV file). If it contains dictionaries or other arrays then the output will not be desirable at all.
dreamlax
Well yeah, but the original poster had the vaguest possible question, so I gave the most generic possible answer. :)
Jonathan Grynspan
@Jonathan Grynspan, thank you for your reply. As the rows are seperated by the '\r\n', I cannot simple use the componentsJoinedByString to solve this problem.
Questions
You haven't specified the nature of the input data, so it's impossible to give you a block of code that transforms it into the output data. Your question needs to be made more specific.
Jonathan Grynspan
+2  A: 

The CSV parser I wrote (http://github.com/davedelong/CHCSVParser) can parse CSV files into an NSArray of NSArrays (if you use the NSArray category included with the code), and I just added a method to the category to take one of those arrays and write it back to a CSV file.

So if you have an NSArray of NSArrays, then you can write it to a CSV file like this:

[myArray writeToCSVFile:myCSVFile atomically:YES];

If will gracefully fail if you do not pass an NSArray of NSArrays.

edit (prompted by @Aran's comment to the question) any object inside the subarrays will have its -description method written to the CSV file. If you want to write a series of objects to the file, with various properties as the fields of the CSV row, then my wrapper requires that the properties be placed in an array, and then that array will make up the row.

edit #2 I just updated my CSV parser to refactor the writing into it's own class. You can now 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];

That will write your csvArray to the file whose path is outputFile, and the file will look like:

First Name,Last Name,Phone
Tom,Chan,123
Peter,Wong,456
Mary's,"Cho""w",789
Dave DeLong
@Dave DeLong, thank you for your reply. And I have updated the post. Would you mind to take a look. thank you.
Questions
@MarkSiu updated answer
Dave DeLong
@Dave DeLong, thank you for your reply. It is very useful. But is it possible to use the code rather than create a object called 'CHCSVWriter'? As I cannot use that. Thank you.
Questions
@MarkSiu why not? The code is free to use...
Dave DeLong
@Dave DeLong, thank you for your reply. I would like to ask is this related to the license problems ? And is only import the CHCSVWriter.h and .m is ok?
Questions
@MarkSiu yes, you only need to include `CHCSVWriter.h` and `CHCSVWriter.m` in your project (as long as you only want to create CSV files). If you want to read CSV files, you'd also need `CHCSVParser.h` and `CHCSVParser.m`. You're free to use it however you like as long as you retain the copyright notice (simple, permissive MIT license).
Dave DeLong
@Dave DeLong, thank you for your reply and the comment is very helpful. Thanks again.
Questions