简体   繁体   English

Swift 3-“ NSPersistentContainer仅在ios 10和更高版本上可用”

[英]Swift 3 - “NSPersistentContainer is only available on ios 10 and newer”

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). 因此,当我尝试将部署目标设置为iOS 9时(确实低于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. 查看不同的SO问题/答案,很明显,我需要添加一些代码来区分iOS 10是否可用。 Swift 3 itself suggests using @available(iOS 10.0, *) . Swift 3本身建议使用@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. 我期望这是因为我错过了一个“如果不可用,请使用此功能”,但是对于Swift和iOS编程来说,我是新手,所以我不确定该写些什么。 And I can't seem to find any answer giving the exact answer regarding what to write. 而且我似乎找不到任何答案,无法给出关于所写内容的确切答案。 Can someone help? 有人可以帮忙吗?

I was facing same issue and I resolved by the 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" 在进口APpDelegate通过这条线"import INSPersistentContainer"
  • Then replace the NSPersistentContainer with INSPersistentContainer 然后将NSPersistentContainer替换为INSPersistentContainer

And that's all. 就这样。

Now I can run my App in lower version of iOS (< iOS 10). 现在,我可以在较低版本的iOS(<iOS 10)中运行我的App。

You really don't need to distinguish iOS 10 from earlier versions. 您确实不需要将iOS 10与早期版本区分开。 NSPersistentContainer requires iOS 10, but the older techniques still work and are still supported. NSPersistentContainer需要iOS 10,但是较旧的技术仍然有效并且仍受支持。 If you need to support iOS <10, don't use NSPersistentContainer . 如果您需要支持iOS <10,请不要使用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使用单个代码路径。

NSPersistentContainer is only available from iOS 10 NSPersistentContainer仅在iOS 10中可用

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 Stack,该版本在iOS 10中仍然可以使用

The other solution would be use the #available attribute in swift to execute the Core Data Stack or the NSPersistentContainer. 另一种解决方案是迅速使用#available属性执行Core Data Stack或NSPersistentContainer。

You can follow https://stackoverflow.com/a/42392120/8023444 this answer to support both ios 9 and ios 10 您可以按照https://stackoverflow.com/a/42392120/8023444此答案来支持ios 9和ios 10

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

相关问题 &#39;NSPersistentContainer&#39;仅适用于iOS 10.0或更高版本 - 'NSPersistentContainer' is only available on iOS 10.0 or newer NSPersistentContainer仅在10.0或更新版本中可用:错误 - NSPersistentContainer is only available in 10.0 or newer : error Swift 3核心数据 - init(context :)仅适用于iOS 10或更高版本 - Swift 3 Core Data - init(context:) is only available on iOS 10 or newer self.locationManager.requestAlwaysAuthorization()仅在iOS 8.0或更高版本上可用-Swift - self.locationManager.requestAlwaysAuthorization() is only available on iOS 8.0 or newer - Swift XCUIApplication仅适用于iOS 9.0或更高版本的swift3 - XCUIApplication is only available on iOS 9.0 or newer, swift3 Swift 编译器错误 (Xcode):Concurrency is only available in iOS 15.0.0 or newer - Swift Compiler Error (Xcode): Concurrency is only available in iOS 15.0.0 or newer NSPersistentContainer和iOS10单元测试 - NSPersistentContainer & UnitTests with iOS10 Xcode 10'frame'仅适用于iOS 12.0或更新的编译错误iOS 12 beta - Xcode 10 'frame' is only available on iOS 12.0 or newer compilation error iOS 12 beta &#39;LABiometryType&#39;仅适用于iOS 11.0.1或更高版本 - 'LABiometryType' is only available on iOS 11.0.1 or newer “ AppDelegate”仅在iOS 10.0或更高版本上可用 - 'AppDelegate' is only available on iOS 10.0 or newer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM