I have no idea why I'm getting this error. Here is my code for the App, the page where I think the problem is occurring, and my class:
App File:
@main
struct To_DoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.environment(IconNames())
}
.modelContainer(for: Todo.self)
}
}
@Observable
class IconNames {
var iconNames: [String?] = [nil]
var currentIndex = 0
init() {
getAlternateIconNames()
if let currentIcon = UIApplication.shared.alternateIconName {
self.currentIndex = iconNames.firstIndex(of: currentIcon) ?? 0
}
}
func getAlternateIconNames() {
if let icons = Bundle.main.object(forInfoDictionaryKey: "CFBundleIcons") as? [String: Any],
let alternateIcons = icons["CFBundleAlternateIcons"] as? [String: Any] {
for (_, value) in alternateIcons {
guard let iconList = value as? Dictionary<String, Any> else { return }
guard let iconFiles = iconList["CFBundleIconFiles"] as? [String] else { return }
guard let icon = iconFiles.first else { return }
iconNames.append(icon)
}
}
}
}
My class:
@Model
class Todo {
var name: String
var details: String
var priority: String
var date: Date
init(name: String = "", details: String = "", priority: String = "Normal", date: Date = .now) {
self.name = name
self.details = details
self.priority = priority
self.date = date
}
}
And the page where I think the error is occurring:
struct AddTodoView: View {
@Environment(\.modelContext) var modelContext
@Environment(\.dismiss) var dismiss
@State private var name = ""
@State private var details = ""
@State private var priority = "Normal"
@State private var date = Date.now
let priorities = ["High", "Normal", "Low"]
@State private var errorShowing: Bool = false
@State private var errorTitle: String = ""
@State private var errorMessage: String = ""
var theme = ThemeSettings()
var themes: [Theme] = themeData
var body: some View {
NavigationStack {
VStack {
VStack(alignment: .leading, spacing: 20) {
TextField("Todo", text: $name)
.padding()
.background(Color(UIColor.tertiarySystemFill))
.cornerRadius(9)
.font(.system(size: 24, weight: .bold, design: .default))
TextField("Details", text: $details, axis: .vertical)
.padding()
.background(Color(UIColor.tertiarySystemFill))
.cornerRadius(9)
Picker("Priority", selection: $priority) {
ForEach(priorities, id: \.self) {
Text($0)
}
}
.pickerStyle(.segmented)
DatePicker("Date", selection: $date)
Button {
if name != "" || details != "" {
let todo = Todo()
todo.name = name
todo.details = details
todo.priority = priority
todo.date = date
do {
try modelContext.save()
dismiss()
} catch {
print(error.localizedDescription)
}
} else {
errorShowing = true
errorTitle = "Invalid Name"
errorMessage = "Make sure to enter something for\nthe new todo item."
return
}
} label: {
Text("Save")
.font(.system(size: 24, weight: .bold, design: .default))
.padding()
.frame(minWidth: 0, maxWidth: .infinity)
.background(themes[theme.themeSettings].themeColor)
.cornerRadius(9)
.foregroundStyle(.white)
}
}
.padding(.horizontal)
.padding(.vertical, 30)
Spacer()
}
.navigationTitle("New Todo")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
Button {
dismiss()
} label: {
Image(systemName: "xmark")
}
}
.alert(errorTitle, isPresented: $errorShowing) { }
message: {
Text(errorMessage)
}
}
.tint(themes[theme.themeSettings].themeColor)
}
}