答案 0 :(得分:5)
其中一个解决方案是使用https://realm.io/docs/java/latest/#sharing-schemas
并添加
typealias NSPersistentContainer = INSPersistentContainer
typealias NSPersistentStoreDescription = INSPersistentStoreDescription
到您要使用的文件
答案 1 :(得分:3)
不可用表示无法使用。
有两种选择:
NSPersistentStoreCoordinator / NSManagedObjectModel
模式。if #available(iOS 10, *)
答案 2 :(得分:2)
在iOS 10之前
您可以直接从AppDelegate.h
lazy var managedObjectContext: NSManagedObjectContext? = {
// Returns the managed object context for the application (which is already bound to the persistent store
// coordinator for the application.) This property is optional since there are legitimate error
// conditions that could cause the creation of the context to fail.
let coordinator = self.persistentStoreCoordinator
if coordinator == nil {
return nil
}
var managedObjectContext = NSManagedObjectContext()
managedObjectContext.persistentStoreCoordinator = coordinator
return managedObjectContext
来自iOS 10及更新版
此更改并且NSManagedObjectContext
已移至PersistentContainer
属性 viewContext
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: "")
...
...
因此,您需要区分运行应用程序的版本,然后调用正确的函数。 AppDelegate中的ManagedObjectContext或[PersistentContainer viewContext]中的ManagedObjectContext。
btw:小心iOS 10之前的版本教程。
答案 3 :(得分:1)
使用@available
标记,如下所示:
@available(iOS 10.0, *)
lazy var persistentContainer: NSPersistentContainer = ...