作者:天秤蜗牛666 | 来源:互联网 | 2022-12-28 16:49
请求更新,因为此问题当然已针对先前版本得到解答,最新的搜索结果日期为12月16日与之前的iOS 9和10项目产生无关的兼容性.
文档当然是说在开始一个新项目时选择使用核心数据复选框,我没有选择,但现在认为需要添加iCloud +核心数据以将我的应用程序带到下一阶段 - >其中类似NSFileCoordinator和需要NSFilePresenter,因为在我的应用程序UI中,用户会看到一些TOPICS,每个都有三个OPTIONS,用户可以选择一个选项.对于每个主题,UI然后显示已选择每个选项的总用户数和每个选项的总计PERCENTAGE.
现在,每个选项的选择数量和总数的百分比当然只是在我的原生应用程序中计算 - >但实际上需要在像云一样的中心计算,或者最有可能在网站上计算...但是网站...引发NSFileCoordinator和NSFilePresenter已经解决的同时读/写问题.
因此,如果iCloud +核心数据系统可以插入现有的泛在容器数值总计的基本算术计算 - 在接收来自各个用户的写入数值命令时在云中 - 在发送新的无处不在容器数值总和百分比值之前 - 然后我非常感谢在尝试创建和初始化核心数据堆栈时如何修复下面生成的错误.否则猜测我将不得不刮掉Xcode并转到像PhoneGap这样的混合应用程序,如果这是最好的工作.
因此,请参阅核心数据编程指南:
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/InitializingtheCoreDataStack.html#//apple_ref/doc/uid/TP40001075-CH4-SW1
并在我现有项目的开头粘贴以下代码,生成
使用未解析的标识符'persistentContainer'...'managedObjectContext'
......错误.和线
init(completionClosure: @escaping () -> ()) {
...生成
初始化程序只能在类型中声明
import UIKit
import CoreData
class DataController: NSObject {
var managedObjectContext: NSManagedObjectContext
init(completionClosure: @escaping () -> ()) {
persistentCOntainer= NSPersistentContainer(name: "DataModel")
persistentContainer.loadPersistentStores() { (description, error) in
if let error = error {
fatalError("Failed to load Core Data stack: \(error)")
}
completionClosure()
}
}
}
init(completionClosure: @escaping () -> ()) {
//This resource is the same name as your xcdatamodeld contained in your project
guard let modelURL = Bundle.main.url(forResource: "DataModel", withExtension:"momd") else {
fatalError("Error loading model from bundle")
}
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
guard let mom = NSManagedObjectModel(contentsOf: modelURL) else {
fatalError("Error initializing mom from: \(modelURL)")
}
let psc = NSPersistentStoreCoordinator(managedObjectModel: mom)
managedObjectCOntext= NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
managedObjectContext.persistentStoreCoordinator = psc
let queue = DispatchQueue.global(qos: DispatchQoS.QoSClass.background)
queue.async {
guard let docURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).last else {
fatalError("Unable to resolve document directory")
}
let storeURL = docURL.appendingPathComponent("DataModel.sqlite")
do {
try psc.addPersistentStore(ofType: NSSQLiteStoreType, configurationName: nil, at: storeURL, options: nil)
//The callback block is expected to complete the User Interface and therefore should be presented back on the main queue so that the user interface does not need to be concerned with which queue this call is coming from.
DispatchQueue.main.sync(execute: completionClosure)
} catch {
fatalError("Error migrating store: \(error)")
}
}
}
// followed by my existing working code:
class ViewController: UIViewController {
Jay N...
39
转到文件>新文件...在iOS下选择核心数据并选择数据模型,
您仍然需要一些代码,无论何时在项目创建期间选择核心数据,xcode都会自动生成.为了得到它,只需创建一个核心数据选项选中的新项目,并复制所有在**// Mark下编写的代码: - AppDelegate.swift中的核心数据堆栈**注释
并添加
import CoreData
以上
可选的
并且不要忘记在刚刚复制的代码的managedObjectModel函数中复制后更改应用程序的名称
1> Jay N...:
转到文件>新文件...在iOS下选择核心数据并选择数据模型,
您仍然需要一些代码,无论何时在项目创建期间选择核心数据,xcode都会自动生成.为了得到它,只需创建一个核心数据选项选中的新项目,并复制所有在**// Mark下编写的代码: - AppDelegate.swift中的核心数据堆栈**注释
并添加
import CoreData
以上
可选的
并且不要忘记在刚刚复制的代码的managedObjectModel函数中复制后更改应用程序的名称
***更改应用程序的名称***表示`managedObjectModel`(或现在的`persistentContainer`代码)中的模型名称.例如:`persistentCOntainer= NSPersistentContainer(name:"DataModel")`,其中`DataModel`应该替换为您的应用程序的名称,因为这是Core Data创建的商店的默认名称.(注意:用破折号替换名称中的任何空格,因为在名为"我的应用程序"的应用程序中将有一个名为"My-App"的默认商店)
对于稍后出现的人,您可能还想在AppDelegate.swift的applicationWillTerminate()和applicationDidEnterBackground()方法中添加`self.saveContext()`.
2> iOSDevSF..:
如果您像我一样懒惰,那么这里是您需要从新的Core Data项目中复制的所有代码...(为什么要让每个人都创建一个新项目?)。变更YOUR_APP_NAME_HERE
import CoreData
// MARK: - Core Data stack
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
self.saveContext()
}
lazy var persistentContainer: NSPersistentCOntainer= {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let cOntainer= NSPersistentContainer(name: "YOUR_APP_NAME_HERE")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()
// MARK: - Core Data Saving support
func saveContext () {
let cOntext= persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}