Core数据和iOS应用程序中的文本文件 [英] Core Data and textfiles in iOS applications

查看:120
本文介绍了Core数据和iOS应用程序中的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的iOS应用程序,由几个UITableViewControllers组成。视图控制器中显示的信息将来自一个文本文件(我将包含在项目的资源中)。



由于这是我第一次使用Core Data,因此我有几个问题:




  • 文本文件最常见的格式是什么? CSV,XML或其他内容?

  • 导入数据最简单的方法是什么?

    几个备注:




    • 数据是静态的。理想情况下,应用程序只会将数据加载到核心数据(第一次运行应用程序)。

    • 每次额外运行应用程序只会从一些Core数据源


    解决方案

    如果数据以关系方式结构化,那么XML或JSON允许轻松保留该结构,然后轻松解析并保存在Core Data存储中。您需要使用XML或JSON解析器,这将使您的数据变成字典数组(或如果您的数据结构需要它的多个级别)。你将简单地遍历数组,挖掘字典(和子数组和子字典,如果适用),并添加对象到你的商店。



    如果它是平面数据,一个简单的单个表将成为一个单一的实体在Core Data,然后制表符分隔或CSV文本文件是很好的(和制表符分隔更容易解析,如果没有任何标签内数据本身)。然后,您可以抓取单个行,将行拆分为数据位数组(这是制表符分隔的作用是超简单的),为每个行创建一个新对象,将其属性设置为数组元素,并保存上下文。



    XML / JSON版本比在这里写的更复杂 - 搜索SO,你会发现很多例子 - 但这里是制表符分隔版本(假设你没有一个庞大的数据,不能合理地保存在内存中):

      / Standard Core Data设置在这里,抓住managedObjectContext,
    //这就是我所说的
    //然后解析你的文本
    NSString * path = [[NSBundle mainBundle] pathForResource: @YourTextFileNameofType:@txt];
    NSString * content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    NSArray * rows = [content componentsSeparatedByString:@\\\
    ];
    //现在我们有行了,我们可以开始创建对象了
    YourManagedObject * yourManagedObject = nil;
    for(NSString * row in rows){
    NSArray * elements = [row componentsSeparatedByString:@\t];
    YourManagedObject * yourManagedObject =(YourManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@YourManagedObjectinManagedObjectContext:managedObjectContext;
    [YourManagedObject setName:[elements objectAtIndex:0]];
    [YourManagedObject setCountry:[elements objectAtIndex:1]];
    //等你可能需要NSNumberFormatter和/或NSDateFormatter将
    //你的字符串转换为日期和数字,这取决于你的数据类型
    [managedObjectContext save];
    }

    Poof,全部完成。


    I'm creating a simple iOS application consisting of a few UITableViewControllers. The information displayed in the view controllers will come from a text file (that I'll include in the project's Resources). The text file's contents will come from a spreadsheet.

    Since this is my first time working with Core Data I have a few questions:

    • What format is most common for the text file? CSV, XML or something else?
    • What's the easiest way to import the data?

    A few notes:

    • The data is static. Ideally the app will load the data into "Core Data" just once (1st time the app is run).
    • Each additional run of the app will just pull data from some Core Data source (that I'm not completely familiar w/ yet) instead of re-loading it from the textfile.

    解决方案

    If the data is structured in a relational way then XML or JSON allows that structure to be easily preserved and then easily parsed and saved in your Core Data store. You'll need to use an XML or JSON parser, which will turn your data into an array of dictionaries (or multiple levels thereof if your data structure requires it). You'll simply iterate through the array and dig into the dictionaries (and sub-arrays and sub-dictionaries, if appropriate) and add objects to your store as you go.

    If it's flat data, a simple single table that will become a single entity in Core Data, then tab-delimited or CSV text files are fine (and tab-delimited is even easier to parse if there wouldn't be any tabs within the data itself). You can then grab individual rows, break the rows down into an array of data bits (this is where tab delimiting makes is super-simple), create a new object for each row, set its properties to the array elements, and save the context.

    The XML/JSON version is more complex than is worth writing out here -- search SO and you'll find lots of examples -- but here's the tab-delimited version (this assumes you don't have a gigantic ball of data that can't reasonably be held in memory):

    // Standard Core Data setup here, grabbing the managedObjectContext, 
    //     which is what I'll call it
    // Then parse your text
    NSString *path = [[NSBundle mainBundle] pathForResource:@"YourTextFileName" ofType:@"txt"];
    NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
    NSArray *rows = [content componentsSeparatedByString:@"\n"];
    // Now that we have rows we can start creating objects
    YourManagedObject *yourManagedObject = nil;
    for (NSString *row in rows) {
      NSArray *elements = [row componentsSeparatedByString:@"\t"];
      YourManagedObject *yourManagedObject = (YourManagedObject *)[NSEntityDescription insertNewObjectForEntityForName:@"YourManagedObject" inManagedObjectContext:managedObjectContext;
      [YourManagedObject setName:[elements objectAtIndex:0]];
      [YourManagedObject setCountry:[elements objectAtIndex:1]];
      // Etc. You may need an NSNumberFormatter and/or an NSDateFormatter to turn
      //   your strings into dates and numbers, depending on your data types
      [managedObjectContext save];
    }
    

    Poof, all done.

    这篇关于Core数据和iOS应用程序中的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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