Swift 3 -“NSPersistentContainer只能在ios 10和更新版本中使用”

时间:2022-07-01 16:56:19

So I'm getting the error stated in the title when I try to set my deployment target to iOS 9 (really whatever below 10.0).

因此,当我试图将部署目标设置为ios9时(实际上是低于10.0),就会在标题中出现错误。

The problem exists here:

存在的问题:

// MARK: - Core Data stack

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: "Keebin_development_1")
    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
}()

Looking at different SO questions/answers it's clear that I need to add some code to differentiate between whether iOS 10 is available or not. Swift 3 itself suggests using @available(iOS 10.0, *). It's not enough though. I expect it's beacuse I'm missing a "if not available use this", however being new to Swift and iOS programming I am not sure what to write specifically. And I can't seem to find any answer giving the exact answer regarding what to write. Can someone help?

查看不同的问题/答案很明显,我需要添加一些代码来区分iOS 10是否可用。Swift 3本身建议使用@available(iOS 10.0, *)。不过这还不够。我想这是由于我错过了一个“如果不能使用这个”,但是作为Swift和iOS编程的新手,我不知道具体要写什么。我似乎找不到任何答案来给出确切的答案。有人可以帮忙吗?

3 个解决方案

#1


1  

You really don't need to distinguish iOS 10 from earlier versions. NSPersistentContainer requires iOS 10, but the older techniques still work and are still supported. If you need to support iOS <10, don't use NSPersistentContainer. It's more steps to load the managed object model and add the persistent store, but doing so means you can have a single code path for all versions of iOS.

你真的不需要区分iOS 10和早期版本。NSPersistentContainer要求使用ios10,但较老的技术仍然有效,仍然受到支持。如果需要支持iOS <10,请不要使用NSPersistentContainer。这是加载托管对象模型和添加持久性存储的更多步骤,但是这样做意味着您可以为所有版本的iOS拥有一个单一的代码路径。

#2


5  

I was facing same issue and I resolved by below mentioned way. I was doing project in Swift 3.0

我面临着同样的问题,我用下面提到的方式解决了。我在做Swift 3.0的项目

  • I used pod 'INSPersistentContainer' and installed the pod in my project.
  • 我使用pod 'INSPersistentContainer'并在我的项目中安装了pod。
  • Imported in APpDelegate by this line "import INSPersistentContainer"
  • 通过“导入INSPersistentContainer”行导入APpDelegate中
  • Then replace the NSPersistentContainer with INSPersistentContainer
  • 然后将NSPersistentContainer替换为INSPersistentContainer

And thats all.

事儿都没有。

Now I can run my App in lower version of iOS (< iOS 10)

现在我可以在iOS下版运行我的App (< ios10)

Hope it will help you. Happy coding...

希望它能对你有所帮助。快乐的编码…

#3


1  

NSPersistentContainer is only available from iOS 10

NSPersistentContainer只能在ios10中使用

If you still need your app working in previous versions you can use the old Core Data Stack which is still work in iOS 10

如果您仍然需要您的应用程序在以前的版本中工作,您可以使用旧的Core Data栈,它仍然在ios10中工作

The other solution would be use the #available attribute in swift to execute the Core Data Stack or the NSPersistentContainer.

另一种解决方案是使用swift中的#available属性来执行核心数据堆栈或NSPersistentContainer。

You can follow https://*.com/a/42392120/8023444 this answer to support both ios 9 and ios 10

您可以使用https://*.com/a/42392120/8023444来支持ios9和ios10

#1


1  

You really don't need to distinguish iOS 10 from earlier versions. NSPersistentContainer requires iOS 10, but the older techniques still work and are still supported. If you need to support iOS <10, don't use NSPersistentContainer. It's more steps to load the managed object model and add the persistent store, but doing so means you can have a single code path for all versions of iOS.

你真的不需要区分iOS 10和早期版本。NSPersistentContainer要求使用ios10,但较老的技术仍然有效,仍然受到支持。如果需要支持iOS <10,请不要使用NSPersistentContainer。这是加载托管对象模型和添加持久性存储的更多步骤,但是这样做意味着您可以为所有版本的iOS拥有一个单一的代码路径。

#2


5  

I was facing same issue and I resolved by below mentioned way. I was doing project in Swift 3.0

我面临着同样的问题,我用下面提到的方式解决了。我在做Swift 3.0的项目

  • I used pod 'INSPersistentContainer' and installed the pod in my project.
  • 我使用pod 'INSPersistentContainer'并在我的项目中安装了pod。
  • Imported in APpDelegate by this line "import INSPersistentContainer"
  • 通过“导入INSPersistentContainer”行导入APpDelegate中
  • Then replace the NSPersistentContainer with INSPersistentContainer
  • 然后将NSPersistentContainer替换为INSPersistentContainer

And thats all.

事儿都没有。

Now I can run my App in lower version of iOS (< iOS 10)

现在我可以在iOS下版运行我的App (< ios10)

Hope it will help you. Happy coding...

希望它能对你有所帮助。快乐的编码…

#3


1  

NSPersistentContainer is only available from iOS 10

NSPersistentContainer只能在ios10中使用

If you still need your app working in previous versions you can use the old Core Data Stack which is still work in iOS 10

如果您仍然需要您的应用程序在以前的版本中工作,您可以使用旧的Core Data栈,它仍然在ios10中工作

The other solution would be use the #available attribute in swift to execute the Core Data Stack or the NSPersistentContainer.

另一种解决方案是使用swift中的#available属性来执行核心数据堆栈或NSPersistentContainer。

You can follow https://*.com/a/42392120/8023444 this answer to support both ios 9 and ios 10

您可以使用https://*.com/a/42392120/8023444来支持ios9和ios10