如何将NSMutableArray转换为CSV文件在iPhone上? [英] How to convert the NSMutableArray to CSV file on iPhone?

查看:307
本文介绍了如何将NSMutableArray转换为CSV文件在iPhone上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个iPhone应用程序,其中包含一个函数。它可以将NSMutableArray转换为CSV文件。但是,我不知道该怎么办。任何人都可以帮助我这样做?非常感谢。



// ------更新-----



谢谢你为大家回复。



实际上,数组包含元素的对象,但我可以把它所有的数组像下面(我想这更容易



数组是NSMutableArray * csvArray,数组包含数据,如下例所示。

  csvArray [0] =名字
csvArray [1] =姓氏
csvArray [2] =电话
csvArray [3] = Tom
csvArray [4] = Chan
csvArray [5] = 123
csvArray [6] = Peter
csvArray [7] = Wong
csvArray [8] = 456
csvArray [9] = Mary's
csvArray [10] = Chow
csvArray [11] = 789 ...

在数组的开头有3个选项卡,它是名字,姓和电话。对于数据,它还包含和,符号。因此,我不能只是通过','符号切割数组。



我想输出的格式如下:

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

名字,姓氏,电话//< - it have在每行数据之间的\r\\\

Tom,Chan,123
Peter,Wong,456
Mary's,Chow,789 ...

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


解决方案

我撰写的CSV解析器( http ://github.com/davedelong/CHCSVParser )可以将CSV文件解析为NSArray的NSArrays(如果您使用代码中包含的NSArray类别),我只是添加了一个方法到类别中,



所以如果你有一个 NSArray NSArrays ,那么您可以将其写入到以下CSV文件中:

  [myArray writeToCSVFile :myCSVFile atomically:YES]; 



如果不传递NSArray的NSArray,



编辑(由@ Aran对问题的注释提示)子阵列中的任何对象将有 -description 方法写入CSV文件。如果要将一系列对象写入文件,并将各种属性用作CSV行的字段,则我的包装器需要将属性放置在数组中,然后该数组将构成该行。



编辑#2 我刚刚更新了我的CSV解析器,重写了它自己的类。现在可以执行以下操作:

  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];

这样会将 csvArray 其路径为 outputFile ,文件将如下所示:

 姓名,电话号码
Tom,Chan,123
Peter,Wong,456
Mary's,Chow,789
/ pre>

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 ...

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

解决方案

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

这篇关于如何将NSMutableArray转换为CSV文件在iPhone上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆