Reputation: 636
This one is testing my patience, I simply want to pass a Coredata entity object to another view, but when I pass it on the sheet I get nil!!!
process:
has anybody experienced this error? how did you fix it? do CData needs some special treatment?
writing some more stuff because stackoverflow doesn't let me post this question if I don't have enough text
import SwiftUI
struct TodayView: View {
@Environment(\.managedObjectContext) var viewContext
@FetchRequest var fetchRequest: FetchedResults<ToDoItem>
@EnvironmentObject var model: ContentModel
@State var taskToEdit: ToDoItem? //tp pass tpo the item detail view
@State var isPresented: Bool = false
init() {
_fetchRequest = FetchRequest<ToDoItem>(sortDescriptors: [NSSortDescriptor(keyPath: \ToDoItem.timestamp, ascending: false)],predicate: NSPredicate(format: "today == true && done == false"))
}
var body: some View {
VStack{
Text("Today").font(.title)
List {
ForEach(fetchRequest, id: \.self) { task in
Text(task.desc!)
.foregroundColor(task.done ? .white : task.today ? .white : .black)
.listRowBackground(task.done ? Color.green : task.today ? Color.blue : Color.white)
.onTapGesture {
taskToEdit = task
isPresented.toggle()
}
.swipeActions(allowsFullSwipe: true) {
Button(role: .cancel) {
task.done.toggle()
task.today = false
task.date_completion = Date()
do {try viewContext.save()} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
print("mark as done")
} label: {
Label("Done", systemImage: "checkmark")
}
.tint(.green)
}
.simultaneousGesture(LongPressGesture()
.onEnded { _ in
let impactMed = UIImpactFeedbackGenerator(style: .light)
impactMed.impactOccurred()
task.today.toggle()
do {try viewContext.save()} catch {
let nsError = error as NSError
fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
}
print("Loooong")
print(task.today)
}
)
}
.onDelete(perform: deleteItems)
}
.listRowSeparator(.hidden)
.listRowInsets(.init(top: 4, leading: 8, bottom: 4, trailing: 8))
.sheet(isPresented: $isPresented, onDismiss: {
isPresented = false
}) {
itemDetail(taskItem: taskToEdit, textFieldText: taskToEdit!.detail!, isPresented: $isPresented)
}
}
}
Upvotes: 0
Views: 71
Reputation: 636
i changed my sheet to as suggested by Vadian and it worked. what the heck!
.sheet(item: self.$taskToEdit, content: { taskToEdit in
itemDetail(taskItem: taskToEdit, textFieldText: taskToEdit.detail ?? "")
})
Upvotes: 0