开发者

Core Data and textfiles in iOS applications

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.


If the data doesn't change, why bother including the text file in the app? Instead, create Core Data file on your Mac and include that as a resource in the app. I presume it's a lot of data that'll take a while to parse, so there's no sense in making your users each wait for that to happen when you could do the parsing once and distribute the result.

To make that happen, take the data model and the parsing code from your app and use them to build a small command-line app that just reads the text file, writes the Core Data file, and exits.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜