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)
}
}
2
Answers
The
@State
property wrapper is NOT meant to be used inside a data model, like your struct Task. The@State
property wrapper is a property wrapper meant to be used on SwiftUI Views. If you remove@State
, you should be in good shape.Do not use
@State
outside aView
like you have inTask
. They are only to be used inside aView
.The docs explaining
@State
is quite useful:Note: that Swift also has a
Task
struct, that is a unit of asynchronous work. This may become a problemif you use the same name for your
Task
struct.