如何将Core Data实体导出到CSV文件 [英] How to export Core Data entity to a CSV file

查看:658
本文介绍了如何将Core Data实体导出到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要将Core Data实体汇出为CSV档案,以储存应用程式的资料。我googled,但没有发现。

I'd like to be able to export my Core Data entity to a CSV file, to save application's data. I googled but found nothing.

我的应用程式只使用一个具有五个属性的实体。这些属性都是字符串,除了一个十进制数字(带逗号或点)。如何导出具有所有属性的实体?

My app uses only one entity with five attributes. These attributes are all strings except one that is a decimal number (with a comma or a point). How can I export this entity with all the attributes?

推荐答案

您必须创建一个方法来执行此类操作,例如this:

You have to create a method to do this kind of operation, like this:

NSString *separator = @", ";
NSString *cvs = @"";
for (NSObject *object in arrayOfObject) {
        cvs = [NSString stringWithFormat:@"%@%@%@%@%@\n", cvs, [object att1], separator, [object att2], separator, [object att3]...];
}
//If you want to store in a file the CVS
[cvs writeToFile:pathToFile atomically:YES];

我建议你尽量避免我使用的追加系统,这种操作会在内存中产生泄漏。
而不是一个stringWithFormat重写相同的var,使用这个写得很好的方法:
如何在Objective-C中连接字符串?

I suggest you to try to avoid the "appending" system that I'm using, this kind of operation generate leaks in memory. Instead of a stringWithFormat that rewrite the same var, use this method that has been very well written: How do I concatenate strings in Objective-C?

你也可以看看这个库: https://github.com/davedelong/CHCSVParser
我从来没有使用它,但它看起来非常有助于写/读取CVS文件。

You can also take a look at this library: https://github.com/davedelong/CHCSVParser I never used it, but it looks very helpful for write/read CVS files.

这篇关于如何将Core Data实体导出到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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