0

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.

1 Answers1

0

Try this approach, using @State var quests and List($quests) { $quest..., works for me.

struct ContentView: View {
    
    // --- here
    @State var quests: [Quest] = [
        Quest(title: "Zimmer aufräumen", done: true),
        Quest(title: "Müll rausbringen", done: false),
        Quest(title: "Bad putzen", done: false)
    ]
         
    var body: some View {
        VStack{
            List($quests) { $quest in  // <--- here
                Button {
                    quest.done.toggle()  // <--- here
                } label: {
                    HStack{
                        if quest.done {  // <--- here
                            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)

                        }
                    }
                }
            }
        }
    }

}

For more info on List with editable data see: https://developer.apple.com/documentation/swiftui/list#listing-editable-data