onmyway133 / blog

🍁 What you don't know is what you haven't learned
https://onmyway133.com/
MIT License
669 stars 33 forks source link

How to allow multiple selection in List in SwiftUI #878

Open onmyway133 opened 2 years ago

onmyway133 commented 2 years ago

Note that

class BookViewModel: ObservableObject {
    @Published var books: [Book] = []
    @Published var selectedBooks: Set<Book> = []
}

List(selection: $viewModel.selectedBooks) {
    ForEach(viewModel.books, id: \.self) { book in
        BookRow(book: book)
    }
}