如何在 iPhone 上将 NSMutableArray 转换为 CSV 文件?

发布于 2024-09-13 14:08:48 字数 834 浏览 2 评论 0原文

我正在编写一个 iPhone 应用程序,其中包含一个函数。它可以将 NSMutableArray 转换为 CSV 文件。但是,我不知道该怎么办。谁能帮我做到这一点?非常感谢。

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

谢谢大家的回复。

实际上,数组包含元素的对象,但我可以将其全部转换为数组,如下所示(我想这样做更容易)。

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

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

数组的开头有 3 个选项卡,分别是名字、姓氏和电话。对于数据,它还包含 " 和 , 符号。因此,我不能只用 ',' 符号来剪切数组。

我想要输出的格式如下

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

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不忘初心 2024-09-20 14:08:48
NSString *csv = [myArray componentsJoinedByString: @","];

但请注意,这不会考虑数组元素中的逗号。您需要为此定义一些转义/取消转义技术,这完全取决于您。

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.

本宫微胖 2024-09-20 14:08:48

我编写的 CSV 解析器(http://github.com/davedelong/CHCSVParser)可以将 CSV 文件解析为NSArray 的 NSArray(如果您使用代码中包含的 NSArray 类别),我刚刚向该类别添加了一个方法来获取其中一个数组并将其写回 CSV 文件。

因此,如果您有 NSArraysNSArray,那么您可以将其写入 CSV 文件,如下所示:

[myArray writeToCSVFile:myCSVFile atomically:YES];

如果您不传递 NSArrays 的 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 的文件,该文件将如下所示:

First Name,Last Name,Phone
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
提笔书几行 2024-09-20 14:08:48

更好的方法是使用 xlsx 而不是 csv...这很简单:

您可以使用 XlsxReaderWriter 库.. .它是免费的并且对于阅读和使用很有用。写!

  • 创建新项目或打开现有项目
  • 插入 XlsxReaderWriter.xcodeproj 作为项目的子项目
  • 在目标构建阶段插入 XlsxReaderWriter 作为目标依赖项
  • 在使用库链接二进制文件中添加 libXlsxReaderWriter.a 和 libz.tbd。较旧的系统可以使用 libz.dylib 而不是 libz.tbd。
  • 在项目设置中的链接/其他链接器标志中添加 -all_load
  • 将 XlsxReaderWriter 根目录路径添加到用户标头搜索路径并将其设置为递归。例如,将路径设置为“$(SRCROOT)/XlsxReaderWriter/”,而不是“$(SRCROOT)/XlsxReaderWriter/XlsxReaderWriter/”。

现在,您可以在代码中导入 BRAOfficeDocumentPackage.h。

它还支持 Swift:

Better approach is use xlsx instead of csv... It is easy:

You can use XlsxReaderWriter library...It is free and usefull for read & write!

  • Create a new project or open an existing project
  • Insert XlsxReaderWriter.xcodeproj as a sub project of your project
  • In your target Build phases insert XlsxReaderWriter as a target dependency
  • Add libXlsxReaderWriter.a and libz.tbd in Link binary with Libraries. Older systems can use libz.dylib instead of libz.tbd.
  • Add -all_load in Linking / Other Linker Flags in your project settings
  • Add the XlsxReaderWriter root directory path to User Header Search Paths and set it as recursive. For example, set the path to "$(SRCROOT)/XlsxReaderWriter/", not "$(SRCROOT)/XlsxReaderWriter/XlsxReaderWriter/".

Now, you can import BRAOfficeDocumentPackage.h in your code.

It also supports Swift:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文