So I have this structure Task which has a @State completion:
struct Task:Identifiable{
var id: Int
@State var completion:Bool = false
var priority:String? = nil
@State var completionDate:Date? = nil
var creationDate:Date
var fullDescription:String
}
In a TaskItem:View I'm trying to make a checkbox that will update the state properly:
struct TaskItem: View {
@State var task:Task
@State var isChecked:Bool = false
func toggle(){
isChecked = !isChecked
task.completion = isChecked
if isChecked {
task.completionDate = Date()
} else {
task.completionDate = nil
}
}
var body: some View {
Button(action: toggle){
Image(systemName: isChecked ? "square.split.diagonal.2x2": "square")
}
}
}
But even though isChecked changes and the image as well, the task.completion doesn't. How can I fix this?
I'm currently using this to view it:
struct TaskItem_Previews: PreviewProvider {
static var previews: some View {
let task = testTask()
TaskItem(task: task)
}
}