I was converting a SwiftUI view to use persistence with SwiftData when I got 17 errors in Xcode! I have no idea what happened when I got those errors, and there were too many to search up, so I decided to ask here. So can anyone help me? Here is the code:
//
// TodoListView.swift
// Vision To-Do
//
// Created by Nicolas Tama on 9/3/23.
//
import SwiftUI
import SwiftData
struct TodoListView: View {
@Environment(\.modelContext) private var modelContext
@State var list: TodoList
@State private var showAddTodoAlert: Bool = false
@State private var newTodoTitle: String = ""
var body: some View {
List {
Section("Todo") {
ForEach(list.items.filter { $0.isDone == false }){ item in
HStack {
Button {
item.isDone.toggle()
}
}
} label: {
Image(systemName: item.isDone ? "circle.fill" : "circle")
}
Text(item.title)
Spacer()
}
}
}
Section("Done") {
ForEach(list.items.filter { $0.isDone }){ item in
HStack {
Button {
item.isDone.toggle()
}
} label: {
Image(systemName: item.isDone ? "circle.fill" : "circle")
}
Text(item.title)
Spacer()
}
}
}
.navigationTitle("Details for \(list.title)")
.toolbar {
Button("Add Todo") {
showAddTodoAlert.toggle()
}
}
.alert("Add Todo", isPresented: $showAddTodoAlert) {
TextField("Todo Title", text: $newTodoTitle)
Button("Cancel", role: .cancel, action: {})
Button("Create") {
let todo = TodoItem(title: newTodoTitle)
modelContext.insert(todo)
list.items.append(todo)
}
}
.id(list.id)
Here are the errors:
Post
Replies
Boosts
Views
Activity
When I am converting a VisionOS project to use persistence with SwiftData, I get 2 errors. The 2 errors are the same, they are Cannot find 'item' in scope. No, I am not stupid. All other lines in the code can find it, so why not these two lines? Can you please help? Here is the code:
//
// TodoListView.swift
// Vision To-Do
//
// Created by Nicolas Tama on 9/3/23.
//
import SwiftUI
import SwiftData
struct TodoListView: View {
@Environment(\.modelContext) private var modelContext
@State var list: TodoList
@State private var showAddTodoAlert: Bool = false
@State private var newTodoTitle: String = ""
var body: some View {
List {
Section("Todo") {
ForEach(list.items.filter { $0.isDone == false }){ item in
HStack {
Button {
item.isDone.toggle()
}
}
} label: {
Image(systemName: item.isDone ? "circle.fill" : "circle") //Here is the error
}
Text(item.title) //Here is the error as well
Spacer()
}
Section("Done") {
ForEach(list.items.filter { $0.isDone }){ item in
HStack {
Button {
item.isDone.toggle()
}
} label: {
Image(systemName: item.isDone ? "circle.fill" : "circle")
}
Text(item.title)
Spacer()
}
}
}
.navigationTitle("Details for \(list.title)")
.toolbar {
Button("Add Todo") {
showAddTodoAlert.toggle()
}
}
.alert("Add Todo", isPresented: $showAddTodoAlert) {
TextField("Todo Title", text: $newTodoTitle)
Button("Cancel", role: .cancel, action: {})
Button("Create") {
let todo = TodoItem(title: newTodoTitle)
modelContext.insert(todo)
list.items.append(todo)
}
}
.id(list.id)
}
}
When making an init statement, it will come up with this error: Return from initializer without initializing all stored properties. What does that mean? 🤷🏻♂️ It would be greatly appreciated if you could give me an explanation and a solution. Thanks! 🙏 Here is the code:
//
// Models.swift
// Vision To-Do
//
// Created by Nicolas Tama on 9/2/23.
//
import Foundation
import SwiftData
@Model
class TodoList {
var title: String
@Relationship(deleteRule: .cascade)
var items: [TodoItem]
init(title: String) {
self.title = title
}
}
@Model
class TodoItem {
var title: String
var isDone: Bool
@Relationship(deleteRule: .cascade)
var items: [TodoItem] = []
init(title: String) {
self.title = title
}
}
I was using SwiftData to make a basic testing app, for the purpose of learning Swift. When I added in the @Relationship macro, I received this error: Type 'Schema.Relationship.Option' has no member 'cascade'. I do not understand the error 🤷🏻♂️. Can you help me out here? It would also be appreciated if you could give me a temporary fix. Thank you for your cooperation! 🤗 Here is the code:
import Foundation
import SwiftData
@Model
class TodoList {
var title: String
@Relationship(.cascade)
var items: [TodoItem]
}
@Model
class TodoItem {
let title: String
var isDone: Bool
}