开发者

How to export Core Data entity to a CSV file

开发者 https://www.devze.com 2023-01-30 07:01 出处:网络
I\'d like to be able to export my Core Data ent开发者_开发技巧ity to a CSV file, to save application\'s data. I googled but found nothing.

I'd like to be able to export my Core Data ent开发者_开发技巧ity 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];

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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号