Creating an entity programmatically (master data)

Is there a way to create Entity programmatically in Core Data using swift2? I was looking for him, but I did not find anything.

+6
source share
2 answers

There are only a few guides on the Internet (perhaps only one ).

I'm not a fan of the Xcode GUI tools (Nibs, Storyboards, XCDataModeld, etc.), so creating everything (from the database to the user interface) in the code is a common thing for me. The article referenced by @Lubos (2 minutes after adding a link to it in the comments, hmm ...) was written in ObjC.

So here is the Swift code:

internal var _model: NSManagedObjectModel { let model = NSManagedObjectModel() // Create the entity let entity = NSEntityDescription() entity.name = "DTCachedFile" // Assume that there is a correct // `CachedFile` managed object class. entity.managedObjectClassName = String(CachedFile) // Create the 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 to entity entity.properties = properties // Add entity to model model.entities = [entity] // Done :] return model } 

This code is equal to this CD model (created in the Xcode GUI):

GUI Model

Creating models in code is much more complicated than using a graphical interface.

But, IMO, it's faster and safer than downloading a CoreData model file to get your model (what if the file is missing or the file is damaged?).

"Safer" I mean that you do not need to handle disk I / O errors associated with reading the CoreData model from disk (your model is in code, there is no need for a model file). The average CoreData user simply does not want to handle these errors because it is easier to terminate the application

+12
source

You can programmatically define a basic data model. I found a good example, although it is written in Objective C. I am sure that it works for Swift 2. You just need to rewrite it. It takes a few minutes.

https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/

+3
source

All Articles