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

han*_*ren 8 xcode core-data ios swift swift2

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

tie*_*tie 14

Web上只有一些教程(可能只有一个).

我不是Xcode的GUI工具(Nibs,Storyboards,XCDataModeld等)的粉丝,因此在代码中创建所有内容(从数据库到UI)对我来说很平常.@Lubos引用的文章(我在评论中添加了一个链接后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
}
Run Code Online (Sandbox Code Playgroud)

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

GUI CoreData模型

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

但是,IMO,它比加载CoreData模型文件更快,更安全,以获取您的模型(如果没有文件存在?或文件损坏?).

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


Lub*_*bos 5

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

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