I try to build an app in MacOS SwiftUI, where two different subviews of main contentView shows different parts of Axis element: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:
		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:
Slider (value: $axes[index].at, in: axis.bounds)
to
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?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I have a struct:public typealias CoordUnit = Double
public struct StyledAxis: StyledAxisProtocol {
		public var name: String
		public var bounds: ClosedRange<CoordUnit>
		public var distribution: Double?
...
}
In SpaceAxisProtocol bounds are defined as:
		public protocol SpaceAxisProtocol: Equatable & Hashable {
				var name: String {get set}
				var bounds: ClosedRange<CoordUnit> {get set}
			 init(_ name: String, bounds: ClosedRange<CoordUnit>)
		}
		public protocol StyledAxisProtocol: SpaceAxisProtocol {...}
And I try to edit bounds in a View:
		struct StyledAxisView<SA:StyledAxisProtocol>: View {
				@Binding var axis: SA
				
				public var body : some View {
						VStack {
								// Axis name and distribution works
								HStack(alignment: .lastTextBaseline) {
										TextField("", text: $axis.name)
												.controlSize(.small)
												.font(.headline)
										
										DistributionView(
												value: $axis.distribution)
												.controlSize(.mini)
								}
								// It causes compiler `Cannot assign to property: 'lowerBound' is a 'let' constant`. But it isn't. ?
								HStack {
										ValueView(value: $axis.bounds.lowerBound)
										ValueView(value: $axis.bounds.upperBound)
								}
								...
When axis.bounds.lowerbound and axis.bounds.upperbound became let? How to edit them in View?