swift2でコアデータをプログラムでエンティティを作成する方法はありますか? 私はそれを探しましたが、何か見つけられませんでした。プログラムでエンティティを作成する(コアデータ)
6
A
答えて
3
コアデータモデルをプログラムで定義することは可能です。私はObjective Cで書かれていますが、良い例を見つけました。私はそれがSwift 2でも働いていると確信しています。あなたはただそれを書き直す必要があります。数分かかったはずです。
https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/
12
は、Web上のいくつかのチュートリアルがあります(おそらく唯一のone)。
私はXcodeのGUIツール(Nibs、Storyboards、XCDataModeldなど)のファンではありません。そのため、すべての(DBからUIへ)コードを作成するのは、私の常識です。 @Lubosが参照している記事(2分後、コメントにリンクを追加しました、hmm ...)は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
}
このコードは(XcodeののGUIで作成された)このCDモデルに等しい:
はコードでモデルを作成することははるかにありますGUIを使用するよりも複雑です。
しかし、IMOでは、CoreDataモデルファイルを読み込んでモデルを取得するよりも高速で安全です(ファイルが存在しないか、ファイルが壊れている場合はどうなりますか?)。
「安全な」私はあなたが(あなたのモデルがコードである、モデルファイル内の必要はありません)ディスクからCoreDataモデルの読み込みに関連するディスクIOエラーを処理する必要がないことを意味します。
は正確にあなたが何を意味するアプリケーションを終了することが容易にそのための平均CoreDataのユーザーは、単にこれらのエラーを処理するためにしたくないですか?インスタンス化することを意味しますか? – Lubos
@ hantorenレコードを挿入することを意味しますか?またはエンティティ自体を作成しますか?コアデータはオブジェクトグラフ管理であり、.xcdatamodeldは私が考えるべきことを記述するために必要です。 – Allen
@Allen '必要がない.xcdatamodeld'、[コードにCoreDataモデルの作成]を参照(https://www.cocoanetics.com/2012/04/creating-a-coredata-model-in-code/)。あなたは、コードから全体CoreData構成(モデル、エンティティ、など)を管理することができます。 IMHO、あなたがアクセスDBをしたいときにロードされる '.xcdatamodeld'ファイルを持つよりもはるかに良いのthats。 – b1nary