I am following this iOS core data tutorial, Core Data Tutorial
My Xcode version is 6.1 while the tutorial uses older one. When needs to create a new project for Mac command line, the tutorial says "change the type to “Core Data”", but in my Xcode, there is no such Core Data option.
So, how shall I start this "Core Data" command line project?
I'm doing very much the same thing, with the exact same problem. My solution was to start a new cocoa project, which will give you a checkbox to use Core Data. This will generate all the Core Data Stack access gubbins. The implementation is fairly straight forward from there, except all the work is done in AppDelegate.m. The main() function is replaced by applicationDidFinishLaunching:().. method.
The only changes required are
(NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FailedBankCD" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
and
(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSManagedObjectContext *context = self.managedObjectContext;
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"darn... %@", error);
exit(1);
}
NSError* err = nil;
NSString* dataPath = [[NSBundle mainBundle] pathForResource:@"Banks" ofType:@"json"];
NSArray* Banks = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:dataPath] options:kNilOptions error:&err];
// NSLog(@"Imported Banks: %@", Banks);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"mm/dd/yy";
[Banks enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
FailedBankInfo *failedBankInfo = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankInfo" inManagedObjectContext:context];
failedBankInfo.name = [obj objectForKey:@"name"];
failedBankInfo.city = [obj objectForKey:@"city"];
failedBankInfo.state = [obj objectForKey:@"state"];
FailedBankDetails *failedBankDetails = [NSEntityDescription insertNewObjectForEntityForName:@"FailedBankDetails" inManagedObjectContext:context];
// failedBankDetails.closeDate = [NSDate dateWithString:[obj objectForKey:@"closeDate"]]; //deprecated in yosemite
failedBankDetails.closeDate = [dateFormatter dateFromString:[obj objectForKey:@"closeDate"]];
failedBankDetails.updateDate = [NSDate date];
failedBankDetails.zip = [obj objectForKey:@"zip"];
failedBankDetails.info = failedBankInfo;
failedBankInfo.details = failedBankDetails;
NSError *error;
if (![context save:&error]) {
NSLog(@"darn... %@", [error localizedDescription]);
}
}];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"FailedBankInfo" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (FailedBankInfo *info in fetchedObjects) {
NSLog(@"Name: %@", info.name);
FailedBankDetails *details = info.details;
NSLog(@"Zip: %@", details.zip);
}
}
Best of luck...
EDIT 1: To get the SQLite database that the tutorial proceeds with change if (![coordinator addPersistentStoreWithType:NSXMLStoreType configuration:nil URL:url options:nil error:&error]) { to if (![coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]) {