【问题标题】:Create Entity programmatically (Core Data)以编程方式创建实体(核心数据)
【发布时间】:2024-01-07 07:05:01
【问题描述】:

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

【问题讨论】:

  • 你到底是什么意思?你的意思是实例化一个?
  • @hantoren 你的意思是插入一条记录吗?还是自己创建一个实体?核心数据是一个对象图管理,我认为需要 .xcdatamodeld 来描述。
  • @Allen .xcdatamodeld 不是必需的,请参阅Creating CoreData model in code。您可以通过代码管理整个 CoreData 配置(模型、实体等)。恕我直言,这比在您想要访问数据库时加载 .xcdatamodeld 文件要好得多。

标签: ios xcode swift core-data swift2


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

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

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

    所以,这是一个 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
    }
    

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

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

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

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

    【讨论】:

    • 我怀疑什么是 manageObjectClassName。我正在使用相同但不工作