【问题标题】:Pre-populate data with SwiftUI 2.0 and Core Data使用 SwiftUI 2.0 和 Core Data 预填充数据
【发布时间】:2020-10-24 14:34:13
【问题描述】:

当应用程序仅第一次加载时,如何将 json 中的数据预填充到 SwiftUI 2.0 中的核心数据中。在 SwiftUI 1.0 中,我是在 AppDelegate.swift 中完成的,但现在没有这样的文件了。

【问题讨论】:

  • 这个问题有很多部分 - 解码 JSON,在应用第一次加载时做一些事情(使用 UserDefaults 或类似的东西来跟踪这个),以及使用 CoreData。您可能应该将问题集中在一件事上,例如关于 CoreData 和 SwiftUI 2 应用程序生命周期
  • AppDelegate 仍然存在 - stackoverflow.com/questions/62538110/…

标签: core-data swiftui


【解决方案1】:

添加您自己的AppDelegate.swift

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey :    Any]? = nil) -> Bool {
        return true
    }
}

然后在你的@main View 添加。

@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

适用于 iOS 或

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        
    }
    
}

和

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

适用于 MacOS

【讨论】:

    猜你喜欢
    • 2012-05-05
    • 2013-06-08
    • 2011-04-15
    • 1970-01-01
    • 2011-10-26
    • 2012-04-25
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多