Please consider the following scenario broken down in this example:
struct Item: Hashable, Identifiable{
var id = UUID()
var text: String
var someTrigger: Bool
}
class Items: ObservableObject {
@Published var items = [Item]()
}
Then I have a List view, and a separate row view. The row view should represent one item (consider an item having more properties than just a text, this is only for simplification).
So I thought I could do something like this:
struct ContentView: View {
@ObservedObject public var items = Items()
var body: some View {
NavigationView{
List {
ForEach(items.items, id: \.id) { item in
// option A
ItemRowView(text: item.text, someTrigger: self.$items.items[self.items.items.firstIndex(of: item)!].someTrigger)
// Option B
ItemRowView(item: item)
}
}
}
}
}
While option A works, if I have someTrigger as a binding like @Binding public var someTrigger
. But if I have multiple properties and thus multiple bindings this gets muddy quite fast. So I think passing the item instance instead is the way to go (so option B). But from what I understood I then have to declare item as an @ObservedObject within ItemRowView. But item is a struct rather than a class. Does that mean I have to declare my items as classes? I don't want to start with ugly workarounds and thus am curious about the best practice approach in this situation.
Thanks for all your answers!
Best regards
Leonard