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, , ( , , ):
NSString *path = [[NSBundle mainBundle] pathForResource:@"YourTextFileName" ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
NSArray *rows = [content componentsSeparatedByString:@"\n"];
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]];
[managedObjectContext save];
}
, .