xcode 以编程方式创建实体(核心数据)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35227132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:30:13  来源:igfitidea点击:

Create Entity programmatically (Core Data)

iosxcodeswiftcore-dataswift2

提问by hantoren

Is there a way to create a Entity programmatically on Core Data with swift2? I searched for it, but I doesn't found something.

有没有办法使用 swift2 在 Core Data 上以编程方式创建实体?我搜索了它,但我没有找到任何东西。

回答by tie

There are only a few tutorials on the Web (possibly only one).

网络上只有几个教程(可能只有一个)。

I am not a fan of Xcode's GUI tools (Nibs, Storyboards, XCDataModeld, etc), so creating everything (from DB to UI) in code is usual thing for me. The article referenced by @Lubos (2 minutes after I added a link to it in comments, hmm...) is written in ObjC.

我不喜欢 Xcode 的 GUI 工具(Nibs、Storyboards、XCDataModeld 等),因此在代码中创建所有内容(从 DB 到 UI)对我来说很平常。@Lubos 引用的文章(在我在评论中添加链接后 2 分钟,嗯...)是用 ObjC 编写的。

So, here is a Swift code:

所以,这是一个 Swift 代码:

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 Xcode's GUI):

这段代码等同于这个 CD 模型(在 Xcode 的 GUI 中创建):

GUI CoreData model

GUI CoreData 模型

Creating models in code is much more complicated than using GUI.

在代码中创建模型比使用 GUI 复杂得多。

But, IMO, it is faster and safer than loading CoreData model file to get your model (what if no file exists? or the file is damaged?).

但是,IMO,它比加载 CoreData 模型文件来获取模型更快、更安全(如果文件不存在怎么办?或者文件已损坏?)。

By 'safer' I mean that you don't have to handle disk IO errors related to reading CoreData model from disk (your model is in code, there is no need in model file). Average CoreData user just don't want to handle these errors because its easier to terminate an application

“更安全”是指您不必处理与从磁盘读取 CoreData 模型相关的磁盘 IO 错误(您的模型在代码中,模型文件中不需要)。普通 CoreData 用户只是不想处理这些错误,因为它更容易终止应用程序

回答by Lubos

It is possible to define core data model programmatically. I found a good example, though it is written in Objective C. I am sure it is working also for Swift 2. You just need to rewrite it. Should take a few minutes.

可以以编程方式定义核心数据模型。我找到了一个很好的例子,虽然它是用 Objective C 编写的。我相信它也适用于 Swift 2。你只需要重写它。应该需要几分钟。

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

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