2016-02-05 51 views
6

有沒有辦法在Core Data上用swift2編程創建實體? 我搜索了它,但我沒有找到一些東西。以編程方式創建實體(核心數據)

+0

你究竟是什麼意思?你的意思是實例化一個? – Lubos

+0

@hantoren你的意思是插入一條記錄?或者自己創建一個實體?核心數據是一個對象圖管理,而.xcdatamodeld對描述我認爲是必要的。 – Allen

+1

@Allen'.xcdatamodeld'沒有必要,請參閱[在代碼中創建CoreData模型](https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/)。您可以從代碼管理整個CoreData配置(模型,實體等)。恕我直言,這比擁有'.xcdatamodeld'文件要在訪問數據庫時加載的要好得多。 – b1nary

回答

12

只有網絡上的一些教程(可能只有one)。

我不是Xcode的GUI工具(Nibs,Storyboards,XCDataModeld等)的粉絲,所以在代碼中創建一切(從數據庫到用戶界面)對我來說是平常的事情。 @Lubos引用的文章(我在評論中添加了一個鏈接後2分鐘,嗯......)是用ObjC編寫的。

所以,這裏是一個斯威夫特代碼:

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複雜。

但是,國際海事組織,它比加載CoreData模型文件來獲得你的模型(如果沒有文件存在?或文件被損壞?)更快,更安全。 '更安全'我的意思是,你不必處理與從磁盤讀取CoreData模型相關的磁盤IO錯誤(你的模型在代碼中,模型文件中沒有必要)。一般CoreData用戶不想處理這些錯誤,因爲它更容易終止應用程序

相關問題