Xcode 6.1 Mac OS X new project for command line tool

I Follow This iOS Basic Data Guide, Basic Data Tutorial

My version of Xcode is 6.1, while the tutorial uses an older one. When you need to create a new project for the Mac command line, the tutorial says: "Change the type to" Master data ", but in my Xcode there is no such parameter" Master data ".

So, how do I run this "Core Data" command line project?

+4
source share
3 answers

I am doing the very same thing with the same problem. My solution was to start a new cocoa project that would provide you with a checkbox for using Core Data. This will create all the ruins of Core Stack data access. The implementation is fairly straightforward from there, except that all the work is done in AppDelegate.m. The main () function is replaced by the applicationDidFinishLaunching :() method.

Only the following changes are required:

(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);
    }
}

Good luck ...

EDIT 1: To get the SQLite database that guides the change process if (! [Coordinator addPersistentStoreWithType: NSXMLStoreType configuration: nil URL: url options: nil error: & error]) {in if (! [Coordinator addPersistentStoreWithType: NSSQLiteStoreType configuration: nSSQLite URL: url options: nil error: & error]) {

+3

Cocoa Mac, seiterm. sqlite. Cocoa Touch iOS, , .

, , SQL- ( ). , , " ", → ""

 -com.apple.CoreData.SQLDebug 1 

. , , , - :

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

.

+2

-, iOS Mac OS X. , , ( , NSArrayController Mac, NSFetchedResultsController iOS).

Core Data.

, Core Data, Xcode, - IMO, .

Cocoa , Core Data. , , , , Core Data.

Or you can simply copy / paste the code into a command line application to configure and use the core kernel data stack. Make sure you are importa Core Data module and you should be good to go.

-2
source

All Articles