You initialise the persistentContainer in the init method.
struct YourApp: App {
var persistentContainer: NSPersistentContainer
init() {
let container = NSPersistentContainer(name: "YourApp")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
self.persistentContainer = container
}
var body: some Scene {
WindowGroup {
ContentView()
.environment(\.managedObjectContext, persistentContainer.viewContext)
}
}
}