🌱 List with Bindings from ObservedObject

Source
Code Block
struct Todo: Identifiable {
     let id = UUID()
     var title: String
     var isDone = false
}

class TodoStore: ObservableObject {
    @Published var todos: [Todo] = [.init(title:"Test")]
}

struct ListRow: View {
     @Binding var todo: Todo
     var body: some View {
          Button(action: {
               self.todo.isDone.toggle()
          }) {
            Text("\(todo.title) \(todo.isDone.description)")
          }
     }
}

struct ContentView: View {
     @StateObject var todoStore = TodoStore()

     var body: some View {
          List(todoStore.todos) { todo in
               ListRow(todo: binding(for: todo))
          }
     }
    // from Scrumdinger sample app
    private func binding(for todo: Todo) -> Binding<Todo> {
        guard let scrumIndex = todoStore.todos.firstIndex(where: { $0.id == todo.id }) else {
            fatalError("Can't find scrum in array")
        }
        return $todoStore.todos[scrumIndex]
    }
}
Made by Brandon . If you find this project useful you can donate.