【问题标题】:getting Black Screen while integrating CoreData to my ToDo app Swift将 CoreData 集成到我的 ToDo 应用 Swift 时出现黑屏
【发布时间】:2020-05-24 11:11:57
【问题描述】:

我正在通过在线课程学习 Swift,当我的讲师将 CoreData 集成到她现有的代码中时,她创建了一个新的核心数据模型并复制粘贴了 App Delegate。但在她的 DataModel 中,没有 SceneDelegate 而在我的。

问题是,由于这些差异,我不能做和她一样的事情。因此,我将 SceneDelegate.swift 文件复制到了我的 Xcode 项目中,并从 DataModel App Delegate 中复制了其他内容。

在这个解决方案之后,我得到了一个黑屏。

我不知道我是否应该添加她和我的应用程序委托,但不同之处在于我在核心数据模型应用程序委托中没有 applicationWillResignActive、applicationDidEnterBackgraound 等,但她确实有。

在存在这些差异的情况下,如何将 CoreData 集成到我的项目中?

谢谢!

这是我的应用委托文件

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {


        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {

    }

    func applicationDidEnterBackground(_ application: UIApplication) {

    }

    func applicationWillEnterForeground(_ application: UIApplication) {

    }

    func applicationDidBecomeActive(_ application: UIApplication) {

    }

    func applicationWillTerminate(_ application: UIApplication) {

    }
}

【问题讨论】:

  • 你能在这里分享你的AppDelegate代码吗?
  • 我刚刚更新,顺便说一句,我通过从讲师的应用委托文件中复制应用委托解决了这个问题,但我仍然想了解如何在我未来的项目中解决这个问题
  • @AliErenAK 我遇到了类似的情况,但上下文略有不同。我在这里放了一个详细的答案-stackoverflow.com/questions/60421107/…
  • 这可能会解决这个问题。 stackoverflow.com/a/65453661/1890317

标签: ios swift core-data core-data-migration


【解决方案1】:

你需要这几行代码:

// 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: "TestCoreData")
    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)")
        }
    }
}

将它放在applicationWillTerminate 函数之后,但在 AppDelegate 类中。

在创建 Xcode 项目并选中“使用核心数据”复选框时会自动添加代码。您可以随时创建一个新的 Xcode 项目,选中“使用核心数据”复选框,然后将代码复制并粘贴到自动创建的 AppDelegate 末尾附近。

【讨论】:

    猜你喜欢
    • 2011-03-14
    • 2017-10-03
    • 2023-03-28
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2011-07-02
    • 2017-09-18
    • 1970-01-01
    相关资源
    最近更新 更多