ios - Create Entity programmatically (Core Data) -


is there way create entity programmatically on core data swift2? searched it, doesn't found something.

there few tutorials on web (possibly one).

i not fan of xcode's gui tools (nibs, storyboards, xcdatamodeld, etc), creating (from db ui) in code usual thing me. article referenced @lubos (2 minutes after added link in comments, hmm...) written in objc.

so, here swift code:

internal var _model: nsmanagedobjectmodel {     let model = nsmanagedobjectmodel()      // create entity     let entity = nsentitydescription()     entity.name = "dtcachedfile"     // assume there correct      // `cachedfile` managed object class.     entity.managedobjectclassname = string(cachedfile)      // create attributes     var properties = array<nsattributedescription>()      let remoteurlattribute = nsattributedescription()     remoteurlattribute.name = "remoteurl"     remoteurlattribute.attributetype = .stringattributetype     remoteurlattribute.optional = false     remoteurlattribute.indexed = true     properties.append(remoteurlattribute)      let filedataattribute = nsattributedescription()     filedataattribute.name = "filedata"     filedataattribute.attributetype = .binarydataattributetype     filedataattribute.optional = false     filedataattribute.allowsexternalbinarydatastorage = true     properties.append(filedataattribute)      let lastaccessdateattribute = nsattributedescription()     lastaccessdateattribute.name = "lastaccessdate"     lastaccessdateattribute.attributetype = .dateattributetype     lastaccessdateattribute.optional = false     properties.append(lastaccessdateattribute)      let expirationdateattribute = nsattributedescription()     expirationdateattribute.name = "expirationdate"     expirationdateattribute.attributetype = .dateattributetype     expirationdateattribute.optional = false     properties.append(expirationdateattribute)      let contenttypeattribute = nsattributedescription()     contenttypeattribute.name = "contenttype"     contenttypeattribute.attributetype = .stringattributetype     contenttypeattribute.optional = true     properties.append(contenttypeattribute)      let filesizeattribute = nsattributedescription()     filesizeattribute.name = "filesize"     filesizeattribute.attributetype = .integer32attributetype     filesizeattribute.optional = false     properties.append(filesizeattribute)      let entitytagidentifierattribute = nsattributedescription()     entitytagidentifierattribute.name = "entitytagidentifier"     entitytagidentifierattribute.attributetype = .stringattributetype     entitytagidentifierattribute.optional = true     properties.append(entitytagidentifierattribute)      // add attributes entity     entity.properties = properties      // add entity model     model.entities = [entity]      // done :]     return model } 

this code equal cd model (created in xcode's gui):

gui coredata model

creating models in code more complicated using gui.

but, imo, faster , safer loading coredata model file model (what if no file exists? or file damaged?).

by 'safer' mean don't have handle disk io errors related reading coredata model disk (your model in code, there no need in model file). average coredata user don't want handle these errors because easier terminate application


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -