How to convert the NSMutableArray file to CSV on iPhone?

I am writing an iPhone application that contains a feature. It can convert NSMutableArray to a CSV file. However, I do not know how to do this. Can someone help me do this? Thank you very much.

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

Thanks everyone for the answer.

Actually the array contains objects of these elements, but I can select all of it in the array as follows (I think it is easier to do this).

An array is NSMutableArray * csvArray, and the array contains data, as in 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... 

At the beginning of the array there are three tabs: first name, last name and phone. For data, it also contains the character β€œand”, so I cannot just cut the array with the character β€œ.”.

The format that I would like to output is as follows

 //--------------------------- 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 ... //--------------------------- 
+6
objective-c nsmutablearray csv
source share
3 answers

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 
+17
source share
 NSString *csv = [myArray componentsJoinedByString: @","]; 

But be careful, this will not take into account the commas inside the elements of the array. You will need to define an escaping / unescaping technique for this, and it is entirely up to you.

+17
source share

It is best to use xlsx instead of csv ... This is easy:

You can use the XlsxReaderWriter library ... It is free and useful for reading and writing!

  • Create a new project or open an existing project.
  • Insert XlsxReaderWriter.xcodeproj as a subproject of your project
  • In your target build phase, insert XlsxReaderWriter as the target dependency.
  • Add libXlsxReaderWriter.a and libz.tbd in a binary connection to the libraries. Older systems may use libz.dylib instead of libz.tbd.
  • Add-all_load to Linking / Other Linker Flags in your project settings.
  • Add the XlsxReaderWriter root path to the user header search paths and set it as recursive. For example, set the path to "$ (SRCROOT) / XlsxReaderWriter /" and not "$ (SRCROOT) / XlsxReaderWriter / XlsxReaderWriter /".

Now you can import BRAOfficeDocumentPackage.h into your code.

It also supports Swift:

0
source share

All Articles