I try to build an app in MacOS SwiftUI, where two different subviews of main contentView shows different parts of Axis element:
Subviews works great, everything updates in both ways, but application hangs-up when AxisView will delete one Axis from axes array.
All experimental code (still changing) is available at https://github.com/typoland/GlyphDesignerTest
AxesView looks like this:
After suggestion received on stackoverflow I changed:
to
This way I can delete axis without explosion, but, second view does not live update anymore.
Is it SwiftUI problem? How to deal with this? @Binding arrays is somehow broken?
Code Block struct ContentView: View { @Binding var document: GlyphDesignerDocument var body: some View { HStack { //Axes Sliders Slider causes explosion AxesSlidersView(axes: $document.axes) AxesView(axes: $document.axes, addRows: {document.axes.insert(Axis("z", bounds: 0...1000), at: $0)}, removeRows: {document.axes.remove(at: $0)}, addAxis: {document.axes.append(Axis("z", bounds: 0...1000))}) } }
Subviews works great, everything updates in both ways, but application hangs-up when AxisView will delete one Axis from axes array.
All experimental code (still changing) is available at https://github.com/typoland/GlyphDesignerTest
AxesView looks like this:
Code Block struct AxesView : View { @Binding var axes: [Axis] @State var selected: Int? = nil var addRows: (_ at:Int) -> Void var removeRows: (_ at: Int) -> Void var addAxis: () -> Void var body: some View { VStack { .... Shows Axes } } } struct AxisView: View { @Binding var axis: Axis var insert: () -> Void var delete: () -> Void @Binding var selected: Bool var body: some View { HStack { //Makes ForEach for each Axis, adds buttons to insert and delete, more parameters of an Axis... } } } struct AxesSlidersView: View { @Binding var axes: [Axis] var body: some View { VStack { ForEach(axes.indices, id:\.self) {index in HStack { Text("\(axis.name)") Slider (value: $axes[index].at, in: axis.bounds) } } } } }
After suggestion received on stackoverflow I changed:
Code Block Slider (value: $axes[index].at, in: axis.bounds)
to
Code Block Slider (value: Bound(get: {axis.ataxes[index].at}, set: {axes[index].at = $0}, in: axis.bounds)
This way I can delete axis without explosion, but, second view does not live update anymore.
Is it SwiftUI problem? How to deal with this? @Binding arrays is somehow broken?