Observable Objects | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/15234721-your-second-ios-and-swiftui-app/lessons/16

I wonder what the solution would look like if Book were left as a struct. I tried implementing it by having the BookmarkButton contain a @Binding to a Book (since this view would like to mutate the book, but doesn’t “own” it), but the trouble comes in how the List in ContentView iterates over the books in the Library. While the library is a @State variable, the books that come out of it are not, and can’t be projected into Bindings to pass along. At this point, I can’t think of any way to create the List such that I can pass a Binding<Book> along to the views that need it.

Hi Jessy,

Since we changed the Book type to class, shouldn’t the data be synchronised? And the toggle will change the value of “readMe” after clicking. I have no idea using Combine and ObservedObject/@Published in this part.

struct BookmarkButton: View {
@ObservedObject var book: Book

var body: some View{
    let bookmark = "bookmark"
    Button{
        book.readMe.toggle()
    } label: {
        Image(systemName: book.readMe ? "\(bookmark).fill" : bookmark)
            .font(.system(size: 48, weight: .light))
    }
}

}