core data - Xcode 6.1 Mac OS X new project for command line tool -
i following ios core data tutorial, core data tutorial
my xcode version 6.1 while tutorial uses older one. when needs create new project mac command line, tutorial says "change type “core data”", in xcode, there no such core data option.
so, how shall start "core data" command line project?
i'm doing same thing, exact same problem. solution start new cocoa project, give checkbox use core data. generate core data stack access gubbins. implementation straight forward there, except work done in appdelegate.m. main() function replaced applicationdidfinishlaunching:().. method.
the changes required are
(nsmanagedobjectmodel *)managedobjectmodel { // managed object model application. fatal error application not able find , load 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 initialize 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]; (failedbankinfo *info in fetchedobjects) { nslog(@"name: %@", info.name); failedbankdetails *details = info.details; nslog(@"zip: %@", details.zip); } }
best of luck...
edit 1: sqlite database tutorial proceeds change if (![coordinator addpersistentstorewithtype:nsxmlstoretype configuration:nil url:url options:nil error:&error]) { if (![coordinator addpersistentstorewithtype:nssqlitestoretype configuration:nil url:url options:nil error:&error]) {
Comments
Post a Comment