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

标签 ios xcode swift core-data swift2

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

最佳答案

Web 上只有少数教程(可能只有 one)。

我不喜欢 Xcode 的 GUI 工具(Nibs、Storyboards、XCDataModeld 等),因此在代码中创建所有内容(从 DB 到 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
}

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

GUI CoreData model

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

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

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

关于ios - 以编程方式创建实体(核心数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35227132/

相关文章:

ios - 从 Swift iOS 中的模态视图中获取值(value)

ios - 应用程序可以注册的最大区域数

xcode - GCC 4.2 编译器中未正确声明方法的错误检测不成功

arrays - 使用 swift 将 Parse.com json 数组转换为数组

ios - Firebase-Swift : How to add every created user to a friends list when created?

ios - 如何将 NSArray 转换为整数值

ios - 索引 UITableView,如 Contacts.app

iphone - cell.detailTextLabel.text 不起作用...为什么

ios - 将 Storyboard 中的 UISwipeGuesture 连接到 TableView Controller

当登录页面存在时,iOS 快速可达性应该失败