CoreDataModelDescription

用声明式方法在代码中描述 Core Data 模型。 方便的 Swift Core Data 模型 🙂

用法

使用 CoreDataModelDescription 来描述您的模型。 示例代码描述了此模型

Image of Author, Publication, and Article model

假设您已经定义了 AuthorPublicationArticle,它们是 NSManagedObject 的子类。

let modelDescription = CoreDataModelDescription(
    entities: [
        .entity(
            name: "Author",
            managedObjectClass: Author.self,
            attributes: [
                .attribute(name: "name", type: .stringAttributeType)
            ],
            relationships: [
                .relationship(name: "publications", destination: "Publication", toMany: true, deleteRule: .cascadeDeleteRule, inverse: "author", ordered: true)
            ]),
        .entity(
            name: "Publication",
            managedObjectClass: Publication.self,
            attributes: [
                .attribute(name: "publicationDate", type: .dateAttributeType),
                .attribute(name: "numberOfViews", type: .integer64AttributeType, isOptional: true)
            ],
            relationships: [
                .relationship(name: "author", destination: "Author", toMany: false, inverse: "publications")
            ]),
        .entity(
            name: "Article",
            managedObjectClass: Article.self,
            parentEntity: "Publication",
            attributes: [
                .attribute(name: "text", type: .stringAttributeType)
            ])
    ]
)

let model = modelDescription.makeModel()

动机

在我的文章Core Data and Swift Package Manager中描述了创建此包的动机。

此包仍处于开发的早期阶段。 请提交缺少功能的 issue。 非常欢迎提交 Pull Request。