これは文脈NEXを使用し、CoreData Storage.shareのモデル名のModelCoreDataを置き換えるためであるIOS 9と10
については、次の構造体を使用してください。コンテキスト
輸入財団 インポートCoreData
/// NSPersistentStoreCoordinator拡張 拡張NSPersistentStoreCoordinator {
/// NSPersistentStoreCoordinator error types
public enum CoordinatorError: Error {
/// .momd file not found
case modelFileNotFound
/// NSManagedObjectModel creation fail
case modelCreationError
/// Gettings document directory fail
case storePathNotFound
}
/// Return NSPersistentStoreCoordinator object
static func coordinator(name: String) throws -> NSPersistentStoreCoordinator? {
guard let modelURL = Bundle.main.url(forResource: name, withExtension: "momd") else {
throw CoordinatorError.modelFileNotFound
}
guard let model = NSManagedObjectModel(contentsOf: modelURL) else {
throw CoordinatorError.modelCreationError
}
let coordinator = NSPersistentStoreCoordinator(managedObjectModel: model)
guard let documents = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else {
throw CoordinatorError.storePathNotFound
}
do {
let url = documents.appendingPathComponent("\(name).sqlite")
let options = [ NSMigratePersistentStoresAutomaticallyOption : true,
NSInferMappingModelAutomaticallyOption : true ]
try coordinator.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: url, options: options)
} catch {
throw error
}
return coordinator
}
}
構造体ストレージ{
static var shared = Storage()
@available(iOS 10.0, *)
private lazy var persistentContainer: NSPersistentContainer = {
let container = NSPersistentContainer(name: "ModelCoreData")
container.loadPersistentStores { (storeDescription, error) in
print("CoreData: Inited \(storeDescription)")
guard error == nil else {
print("CoreData: Unresolved error \(String(describing: error))")
return
}
}
return container
}()
private lazy var persistentStoreCoordinator: NSPersistentStoreCoordinator? = {
do {
return try NSPersistentStoreCoordinator.coordinator(name: "ModelCoreData")
} catch {
print("CoreData: Unresolved error \(error)")
}
return nil
}()
private lazy var managedObjectContext: NSManagedObjectContext = {
let coordinator = self.persistentStoreCoordinator
var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
}()
// MARK: Public methods
enum SaveStatus {
case saved, rolledBack, hasNoChanges
}
var context: NSManagedObjectContext {
mutating get {
if #available(iOS 10.0, *) {
return persistentContainer.viewContext
} else {
return managedObjectContext
}
}
}
mutating func save() -> SaveStatus {
if context.hasChanges {
do {
try context.save()
return .saved
} catch {
context.rollback()
return .rolledBack
}
}
return .hasNoChanges
}
func deleteAllData(entity: String)
{
// let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
let managedContext = Storage.shared.context
let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entity)
fetchRequest.returnsObjectsAsFaults = false
do
{
let results = try managedContext.fetch(fetchRequest)
for managedObject in results
{
let managedObjectData:NSManagedObject = managedObject as! NSManagedObject
managedContext.delete(managedObjectData)
}
} catch let error as NSError {
print("Detele all data in \(entity) error : \(error) \(error.userInfo)")
}
}
}
[これをチェック](https://www.google.de/search?q = core + data + stack + ios9&ie = utf-8&oe = utf-8&client = firefox-b-ab&gfe_rd = cr&ei = upZSWN3dH7Go8wfJq5bQDQ) – shallowThought
なぜdownvotedですか?巨大な努力に感謝し、@ shallowThoughtを助けてください... 何とかNSPersistentContainerをスタックに組み込む必要があると思っていました。それが理由です。 – Bastek