iOS:如何使用下载的CSV填充CoreData [英] iOS: How to Populate CoreData with downloaded CSV

查看:133
本文介绍了iOS:如何使用下载的CSV填充CoreData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用一个应用程序,用户每天输入存储在核心数据中的数据(两个属性 NSNumber 和一个 NSDate ),我想通过允许用户通过按钮点击从外部文件(如csv或任何其他支持的格式)导入数据来改进。有关如何有效执行此操作的任何建议吗?

I have been working on an app where the user inputs data stored in core data everyday (two attributes an NSNumber and one as NSDate) and I wanted to improve that by allowing the user to import data from a external file such as csv or any other supported format through a button click. Any suggestions on how to proceed efficiently to do this?

谢谢。

/ strong>只需添加csv文件的截图以及csv解析器的输出为NSArray。

Just adding a screenshot of the csv file as well as the output of the csv parser as NSArray. Basicly need to fetch the attribute separately and store them in core data on button click.

- 输入文件为csv:

- 示例csv解析器输出(NSarray):

推荐答案

我最近需要实现类似的东西。

I needed to achieve something similar recently.

我的项目团队的几个成员希望将我们的应用程序原型展示给潜在客户,但希望向每个客户显示不同的数据。我们通过允许我们的项目团队的成员在与客户端会面之前创建自己的测试数据来解决这个问题。

A couple of members of my project team wanted to take our app prototype out to show potential clients, but wanted to show different data to each client. We solved this by allowing members of our project team to create their own test data before meeting with the client.

我通过创建一个.csv文件示例并分发它给项目团队中的其他人。

I achieved this by creating an example .csv file and distributing it to the other guys in the project team. They populate it with their own test data and use iTunes File Sharing to drop the .csv test data file on to the device.

加载时,应用程序会扫描其Documents目录(如果有的话),然后使用iTunes文件共享将.csv测试数据文件放到设备上。用于测试数据文件。如果存在,它将解析.csv文件并保留到数据库。

On load, the app scans its Documents directory for a the test data file. If it exists, it parses the .csv file and persists to the database.

对于CSV解析,我使用Dave DeLong的CHCSVParser: https ://github.com/davedelong/CHCSVParser

For the CSV parsing, I used Dave DeLong's CHCSVParser: https://github.com/davedelong/CHCSVParser

为您的应用设置iTunes文件共享提供了大量帮助。 Google快速找到此教程( http: //www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app ),如果您需要,应该帮助您。

Plenty of help is available on setting up iTunes file sharing for your app. A quick Google finds this tutorial (http://www.raywenderlich.com/1948/how-integrate-itunes-file-sharing-with-your-ios-app) which should help you out, if you need it.

在Core Data中存储来自.csv的数据的编辑添加帮助

你存储一个NSNumber和NSDate。以此为起点,您可能有以下形式的.csv文件:

You stated in your original post that you store an NSNumber and NSDate. Taking that as a starting point, you might have a .csv file in the following form:

+----------------+--------------+
+ NSNumberColumn | NSDateColumn |
+----------------+--------------+
+        1       |  2013-05-15  |
+        2       |  2013-06-15  |
+        3       |  2013-07-15  |
+----------------+--------------+

假设CSV解析器的输出是NSArray的NSArray,您可以按如下所示创建Core Data对象:

Assuming the output from the CSV parser is an NSArray of NSArrays, you could create the Core Data objects as follows:

我会为列号创建几个宏:

I would create a couple of macros for the column numbers:

#define NSNumberColumn 0
#define NSDateColumn 1

然后迭代.csv文件中的行:

Then iterate over the rows in the .csv file:

NSArray *rows = [NSArray arrayWithContentsOfCSVFile:pathToFile]; //CHCSVParser specific parsing method
for (NSArray *row in rows)
{
    NSString *numberString = [parsedCsvRow objectAtIndex:NSNumberColumn];
    NSString *dateString = [parsedCsvRow objectAtIndex:NSDateColumn];   

    NSNumber *number = // parse numberString to an NSNumber. Plenty of other posts on achieving this. 
    NSDate *date = // parse NSDate from dateString. Plenty of other posts on achieving this.

    NSManagedObjectContext *context = [self managedObjectContext];
    NSManagedObject *myCoreDataObject = [NSEntityDescription insertNewObjectForEntityForName:@"MyCoreDataObject" inManagedObjectContext:context];
    [myCoreDataObject setValue:number forKey:@"NSNumberColumn"];
    [myCoreDataObject setValue:date forKey:@"NSDateColumn"];
    NSError *error;
    if (![context save:&error]) {
        NSLog(@"%@", [error localizedDescription]);
    }
}

注意:输入验证为了简洁,省略了空检查。我也自由组成你的NSManagedObject属性名称,这将需要更新。上面的代码应该分成一个更合适的类结构。

Note: Input validation and null checks have been ommited for brevity. I have also taken the liberty of making up your NSManagedObject property names, this will need updating. The above code should be separated in to a more suitable class structure.

我现在不在Mac,所以很遗憾,我无法检查这是否可行。

I'm not at a Mac right now, so unfortunately I can't check if this works.

希望有帮助。

这篇关于iOS:如何使用下载的CSV填充CoreData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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