I want to make a ToDo-App. So I made a List that takes the data from an Array.
The Array works with a struct and consists of a title (String) and a Boolean which checks if the "quest" is done or not.
In the ContentView you can see the List with the title and left of it an icon which changes depending on how the boolean "done" is.
When I click a line I want that this task is checked. But its not possible to change this Boolean.
import SwiftUI
struct Quest: Identifiable{
var title: String
var done: Bool
var id: String { title }
}
struct ContentView: View {
var quests: [Quest] = [
Quest(title: "Zimmer aufräumen", done: true),
Quest(title: "Müll rausbringen", done: false),
Quest(title: "Bad putzen", done: false)
]
var done = false
var body: some View {
VStack{
List(quests) { quest in
Button {
quest.done = true. // <- Error
} label: {
HStack{
if done == true {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color("checked")) // own Color
Text(quest.title)
.foregroundColor(.gray)
} else {
Image(systemName: "checkmark.circle")
.foregroundColor(Color(.gray))
Text(quest.title)
.foregroundColor(.black)
}
}
}
}
}
}
func done(_ quest: inout Quest) {
quest.done = true
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I have no clue, what I can should do now.