[iOS, Swift] Core Data Model Programmatically in a Swift Package
Data Model File in a XCode Project (.xcdatamodeld)
Data Model
XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="18154" systemVersion="20E241" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="ExampleEntity" representedClassName="ExampleEntity" syncable="YES" codeGenerationType="class">
<attribute name="name" optional="YES" attributeType="String"/>
<attribute name="date" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
</entity>
</model>
Create Core Data Model Programmatically
1. NSManagedObject
public final class ExampleEntity: NSManagedObject {
@NSManaged var name: String
@NSManaged var date: Date
}
2. NSEntityDescription
func entityDescription() -> NSEntityDescription {
let entity = NSEntityDescription()
entity.name = "ExampleEntity"
entity.managedObjectClassName = NSStringFromClass(ExampleEntity.self)
// Attributes
let nameAttr = NSAttributeDescription()
nameAttr.name = "name"
nameAttr.attributeType = .stringAttributeType
nameAttr.isOptional = true
let dateAttr = NSAttributeDescription()
dateAttr.name = "date"
dateAttr.attributeType = .dateAttributeType
entity.properties = [nameAttr,
dateAttr]
return entity
}
3. NSManagedObjectModel
func managedObjectModel() -> NSManagedObjectModel {
let model = NSManagedObjectModel()
model.entities = [entityDescription()]
return model
}
4. NSPersistentContainer
lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentCloudKitContainer(name: "ExampleModel", managedObjectModel: managedObjectModel())
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
Leave a comment