Xcode 6.1 Mac OS X new project for command line tool

2k views Asked by At

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?

3

There are 3 answers

1
h0x0 On BEST ANSWER

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]) {

0
Jody Hagins On

First, you are using an iOS tutorial to develop a Mac OS X application. Core data use is very similar between the two, but there are some obvious difference in how you use it in your application (namely, you generally use NSArrayController for mac apps, whereas you use NSFetchedResultsController for iOS apps).

The template for a command-line application does not have a Core Data checkbox.

To me, this is a benefit, because the Core Data code included with Xcode templates is, IMO, not the best way to use it in your application.

However, that being said, you can create a new Cocoa Application, and check to include Core Data. This is probably enough for what you want to do, which is play around with Core Data.

Or, you can just copy/paste the code into the command line application to setup and use the basic core data stack. Make sure you import the Core Data module, and you should be good to go.

0
Aero On

I ended up creating a Cocoa application for Mac as seiterm suggested. However the database created wasn't in the sqlite format. So I created a Cocoa Touch application for iOS, used the same code and it was fine.

Also, if you are having trouble finding the location of the database, it may be helpful to turn on SQL debugging (covered in one of the other tutorials). This can be done by clicking your scheme at the top, then Edit Scheme, Run config -> Arguments tab and adding

 -com.apple.CoreData.SQLDebug 1 

as an argument. Then when you run the app, there should be an output in the console, something like:

CoreData: annotation: Connecting to sqlite database file at "/Users/doraemon/Library/....."   

which points to the database location.