如何将Core Data实体导出到CSV文件 [英] How to export Core Data entity to a CSV file
问题描述
我希望能够将我的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?
推荐答案
您必须创建一个方法来执行此类操作,例如这个:
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];
我建议您尽量避免我正在使用的附加系统,这种操作会在内存中产生泄漏。
而不是重写相同的var的stringWithFormat,使用这个写得很好的方法:
Objective-C中的快捷键连接NSStrings
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: Shortcuts in Objective-C to concatenate NSStrings
您还可以看看这个库: a href =https://github.com/davedelong/CHCSVParser =nofollow noreferrer> 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屋!