简体   繁体   English

NSPersistentContainer仅在10.0或更新版本中可用:错误

[英]NSPersistentContainer is only available in 10.0 or newer : error

It's because My deployment target is less than 10. 这是因为我的部署目标小于10。

how to resolve for deployment target lower to 10.0 ? 如何解决部署目标低至10.0?

在此输入图像描述

One of solutions is to use https://github.com/inspace-io/INSPersistentContainer 解决方案之一是使用https://github.com/inspace-io/INSPersistentContainer

and add 并添加

typealias NSPersistentContainer         = INSPersistentContainer
typealias NSPersistentStoreDescription  = INSPersistentStoreDescription

to your file where you want to use 到你想要使用的文件

Not available means not available. 不可用表示不可用。

There are two options: 有两种选择:

  • Use only the old NSPersistentStoreCoordinator / NSManagedObjectModel pattern. 仅使用旧的 NSPersistentStoreCoordinator / NSManagedObjectModel模式。
  • Use both patterns and write the code with availability checking if #available(iOS 10, *) if #available(iOS 10, *)请使用这两种模式并使用可用性检查编写代码

Before iOS 10 在iOS 10之前

you could access the NSManagedObjectContext directly from AppDelegate.h 您可以直接从AppDelegate.h访问NSManagedObjectContext

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

( Original code ) 原始代码

from iOS 10 and newer 来自iOS 10及更新版本

this changed and the NSManagedObjectContext has been moved into the PersistentContainer into the attribute viewContext 这已更改, 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: "")
...
...

( original code ) 原始代码

So, you need to distinguish which version you're app is running on and then call the correct function. 因此,您需要区分正在运行的应用程序版本,然后调用正确的函数。 ManagedObjectContext inside AppDelegate or The ManagedObjectContext inside [PersistentContainer viewContext]. AppDelegate中的ManagedObjectContext或[PersistentContainer viewContext]中的ManagedObjectContext。

btw: Be careful with tutorials for versions before iOS 10. 顺便说一句:在iOS 10之前的版本教程要小心。

像这样使用@available标签: @available(iOS 10.0, *) lazy var persistentContainer: NSPersistentContainer = ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 'NSPersistentContainer'仅适用于iOS 10.0或更高版本 - 'NSPersistentContainer' is only available on iOS 10.0 or newer Swift 3-“ NSPersistentContainer仅在ios 10和更高版本上可用” - Swift 3 - “NSPersistentContainer is only available on ios 10 and newer” Flutter:AVCapturePhotoOutput' 仅适用于 iOS 10.0 或更高版本 - Flutter: AVCapturePhotoOutput' is only available on iOS 10.0 or newer “ AppDelegate”仅在iOS 10.0或更高版本上可用 - 'AppDelegate' is only available on iOS 10.0 or newer FormBaseCell'仅在iOS 10.0或更高版本上可用 - FormBaseCell' is only available on iOS 10.0 or newer 如何修复'UITextFieldDidEndEditingReason'仅在iOS 10.0或更高版本上可用 - How to fix 'UITextFieldDidEndEditingReason' is only available on iOS 10.0 or newer 针对iOS 11.0但仍然收到警告:仅适用于iOS 10.0或更高版本 - Targeting iOS 11.0 but still getting warnings: is only available on iOS 10.0 or newer 如何修复错误:init(boundsSize:requestHandler :)'仅在iOS 10.0上可用 - How to fix error: init(boundsSize:requestHandler:)’ is only available on iOS 10.0 Xcode 11 “'==' 仅在 iOS 13.0 或更高版本中可用”错误 - Xcode 11 “'==' is only available in iOS 13.0 or newer” error Xcode UI测试:lldb错误“仅适用于iOS 9.0或更高版本” - Xcode UI Testing: lldb error “only available on iOS 9.0 or newer”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM