I have a working project using SwiftData that is entirely built with UIViews. Essentially, the user will tap an answer on a UIView, and add a row to my SwiftData output. This works as expected. I would like to port this data over to a new project where I’ve added AR functionality, and tapping ImageAnchors in the environment opens up the existing views. I am having trouble figuring out how to define the modelContainer in this new project. There is no UIViewController in my original app, but there is one in my new AR app, which further complicates things for me.
For my existing project’s “App”, I have:
App:
import SwiftUI
import SwiftData
@main
struct QuizTestApp: App {
var body: some Scene {
WindowGroup {
QuizView()
}
.modelContainer(for: SwiftDataController.self)
}
}
Note that the line “.modelContainer(for: SwiftDataController.self) is what I’m essentially trying to set in an AppDelegate.
Model: “SwiftDataController”
import Foundation
import SwiftData
@Model
class SwiftDataController: Identifiable {
var module: Int
var correct: Bool
var dateTaken: Date
init(module: Int, correct: Bool, dateTaken: Date = .now) {
self.module = module
self.correct = correct
self.dateTaken = .now
}
}
The data is added via an addItem method in a UIView, and then displayed in a List in a separate view I can access from a button.
Here is my new project’s AppDelegate:
import UIKit
import SwiftData
@main
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
}
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
I am relatively new to Swift, so any guidance on how to handle this or bypass this blocker would be greatly appreciated. Thanks so much!
I’m unsure if I can have an App and an AppDelegate in the same project, but I have tried some other ways Google has suggested of declaring the modelContainer in the AppDelegate, and my project crashes when I run it.
Daniel Blumberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.