Basic data and text files in iOS applications

I am creating a simple iOS application consisting of several UITableViewControllers. The information displayed in the view controllers comes from a text file (which I will include in the project resources). The contents of the text file will come from the spreadsheet.

Since this is my first experience with Core Data, I have a few questions:

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

A few notes:

  • The data is static. Ideally, the application will only upload data to the "Master Data" once (the first time the application starts).
  • Each additional launch of the application will simply retrieve data from some Core Data source (which I do not know yet), instead of re-loading it from a text file.
+3
source share
2 answers

If the data is structured in a relational way, then XML or JSON makes it easy to save this structure, and then easily analyze and save it in the Core Data repository. You will need to use an XML or JSON parser that turns your data into an array of dictionaries (or several levels if your data structure requires it). You simply iterate over the array and dig into the dictionaries (and sub-arrays and sub-dictionaries, if necessary) and add objects to your store as you go.

, , Core Data, CSV ( , ). , ( , ), , .

XML/JSON , - SO, , ( , , ):

// 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];
}

, .

+3

, ? Core Data Mac . , , , , , .

, , , .

+1

All Articles