Using @State in ParentView and @Binding in ChildView we can make a connection between values of 2 variables in a print and child view. Using code like this:
ChildView(valueFromParent: $valueInParent)
Having valueInParent in ParentView using @State and valueFromParent in ChildView using @Binding. That way those 2 variables are related now and changing one will change another.
Is it possible though to have this binding between two views in a ForEach loop using rows' id numbers? Something like this:
ForEach((1...15), id: \.self) {item in
ChildView(valueFromParent[item]: self.$valueInParent[item])
}
I am aware of another way - creating an array of item to pass to the list with IDs inside an array:
@State private var list = [
(id: 0, model: ListItemModel()),
(id: 1, model: ListItemModel()),
(id: 2, model: ListItemModel()),
(id: 3, model: ListItemModel()),
(id: 4, model: ListItemModel())
]
Edit: But I wish to avoid the above array with a fixed list of items.
I would like to create a simple prototype using just a ForEach loop like this: ForEach((1...15), id: .self). The problem is I don't know how to access a var's value from a particular row in the list.
ForEach(range)is constant container, ie. you won't be able to change its size, ie. adding/removing items in model container either have no effect (on add) or crash (on remove) due to out of range exception. - Asperi