I was recently looking at Apple's sample code for a ReferenceFileDocument
based SwiftUI document app: Building a Document-Based App with SwiftUI
The two main data types, Checklist
and ChecklistItem
, are defined as structs
. There is also a helper struct BindingCollection
that converts a binding to a collection of elements into a collection of bindings to the individual elements. The app relies on bindings to these structs.
However, in some ways, isn't a binding to a struct circumnavigating value-type semantics? Any piece of code that is passed the Binding can alter the value, rather than a copy of the value? How does SwiftUI know that the struct has been updated without a publisher?
Last question: if I'd been writing this app myself, I would have made Checklist
a class, conforming to ObservableObject
publishing its items
property. This would have avoided the helper function, and use of bindings. What are the benefits of the struct / binding approach in the example code?