I’m a complete beginner to Swift, but I’m trying to make some random practice app that uses swiftData. I’m using swift playground on my iPad pro. Anytime I try to save a class model, the app crashes. But this only happens in the preview window. When I actually run the app, the app crashes when I enter a specific navigation link (Schedule, in the following code)
Here is the code from each file in my app.
MyApp:
import SwiftUI
import SwiftData
@main
struct MyApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
ContentView:
import SwiftUI
import SwiftData
struct ContentView: View {
@Environment(\.modelContext) var modelContext
var body: some View {
NavigationView{
VStack(spacing:35){
NavigationLink("Unit Converter"){
VStack(spacing: 50){
NavigationLink("Length"){
UnitConversions.init(unitConversions: ["mm" : 1000.0, "cm" : 100.0, "m": 1.0,
"km" :0.001, "in" : 39.37, "ft" : 3.281, "mi" : Double(1.0/1609.0)], units: ["m", "cm", "mm", "km", "in", "ft", "mi"])
}
NavigationLink("Mass"){
UnitConversions.init(unitConversions: ["g": 1.0, "mg": 1000.0, "kg": 0.001, "ozs" : 0.0353, "lbs" : 0.0022, "tonne" : 0.000001], units: ["g", "mg", "kg", "ozs", "lbs", "tonne"])
}
NavigationLink("Volume"){
UnitConversions.init(unitConversions: ["l": 1.0, "ml" : 1000.0, "tsp" : 202.9, "tbsp" : 67.6, "cup" : 4.23, "quart" : 1.057, "gallon" : 0.2642, "pint" : 2.113], units: ["l", "ml", "tsp", "tbsp", "cup", "quart", "gallon", "pint"])
}
//NavigationLink("Temperature"){
//UnitConversions.init(unitConversions: ["ºC": 1.0, "ºF" : 0.0, "K": 0.0], units: ["ºC", "ºF", "K"])
//}
NavigationLink("Time"){
UnitConversions.init(unitConversions: ["ms" : 3600000.0, "s" : 3600.0, "min" : 60.0, "hrs" : 1.0, "day" : 0.0416666667, "month" : 0.00137, "year" : 0.000114154 ], units: ["hrs", "ms", "s", "min", "day", "month", "year"])
}
.navigationTitle("Units")
}
}
//NavigationLink("Calculator"){
//}
NavigationLink("Schedule"){
Schedule.init(selectedDate: Date.now)
}
NavigationLink("Assignments"){
AssignmentView.init()
}
}
.navigationTitle("Tools")
.modelContainer(for: Assignment.self)
}
}
}
Schedule:
import SwiftUI
import SwiftData
struct Schedule: View {
@State var selectedDate : Date
var body: some View{
VStack() {
Text(selectedDate.formatted(date: .abbreviated, time: .omitted))
.font(.system(size: 40))
.bold()
.foregroundColor(Color.accentColor)
.padding()
.animation(.snappy(), value: selectedDate)
.frame(width: 600)
Divider().frame(height: 1)
DatePicker("Select Date", selection: $selectedDate, displayedComponents: [.date])
.padding(.horizontal)
.datePickerStyle(.graphical)
Divider()
NavigationLink("Add Assignments") {
AddAssignment.init()
}
}
.padding(.vertical, 200)
}
}
UnitConversions (not relevant to my error, but figured i’d throw it in)
import SwiftUI
struct UnitConversions: View {
var unitConversions: [String: Double]
var units: [String]
@State private var unit1 = ""
@State private var unit2 = ""
@State private var unit1Amount = 0.0
var body: some View{
VStack{
TextField("Enter your Number", value: $unit1Amount, format: .number)
.textFieldStyle(.roundedBorder)
.padding()
Picker("Starting Unit", selection: $unit1) {
ForEach(units, id: \.self) {
Text($0)
}
}
.padding()
Picker("Converting Unit", selection: $unit2) {
ForEach(units, id: \.self) {
Text($0)
}
}
.padding()
Text("\((unit1Amount * unitConversions[unit2, default : 1.0] / unitConversions[unit1, default : 1.0]).formatted())")
}
}
}
Assignment:
import Foundation
import SwiftUI
import SwiftData
@Model
class Assignment {
var dueDate: Date
var assignmentName: String
var classroomName: String
init(dueDate: Date = .distantFuture, name: String = "", classroomName: String = ""){
self.dueDate = dueDate
self.assignmentName = name
self.classroomName = classroomName
}
}
AssignmentView:
import SwiftUI
import SwiftData
struct AssignmentView: View {
@Query var assignments: [Assignment]
var body: some View {
VStack {
ForEach(assignments) {assignment in
Text(assignment.classroomName)
}
}
}
}
AddAssignment:
import SwiftUI
import SwiftData
struct AddAssignment: View {
@State var newAssignment = Assignment.init(dueDate: .now, name: "", classroomName: "")
@Environment(\.modelContext) var modelContext
func saveInfo(){
modelContext.insert(newAssignment)
}
var body: some View {
VStack{
TextField("Assignment Name", text: $newAssignment.assignmentName)
.textFieldStyle(.roundedBorder)
.padding()
TextField("For Class", text: $newAssignment.classroomName)
.textFieldStyle(.roundedBorder)
.padding()
Button(
action: {saveInfo()},
label: { Text("Add Assignment") }
)
}
}
}
Since I’m a beginner, I followed Hacking with Swift’s tutorial for swiftData. I’ve been trying to fix this for a long time, but I really don’t know what it is. Anyone with more knowledge, could you ELI5 what I’m doing wrong?
EDIT: The error I’m getting when it crashes is:
A fatal error was found in ModelContainer.swift at line 144, failed to find a currently active container for Assignment