Post

Replies

Boosts

Views

Activity

Same array binded to two SwiftUI Views hangs-up App when object deleted
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?
0
0
605
Nov ’20
Cannot assign to property: 'lowerBound' is a 'let' constant — becomes 'let' in UIView
I have a struct:public typealias CoordUnit = Double public struct StyledAxis: StyledAxisProtocol { &#9;&#9;public var name: String &#9;&#9;public var bounds: ClosedRange<CoordUnit> &#9;&#9;public var distribution: Double? ... } In SpaceAxisProtocol bounds are defined as: &#9;&#9;public protocol SpaceAxisProtocol: Equatable & Hashable { &#9;&#9;&#9;&#9;var name: String {get set} &#9;&#9;&#9;&#9;var bounds: ClosedRange<CoordUnit> {get set} &#9;&#9;&#9; init(_ name: String, bounds: ClosedRange<CoordUnit>) &#9;&#9;} &#9;&#9;public protocol StyledAxisProtocol: SpaceAxisProtocol {...} And I try to edit bounds in a View: &#9;&#9;struct StyledAxisView<SA:StyledAxisProtocol>: View { &#9;&#9;&#9;&#9;@Binding var axis: SA &#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;public var body : some View { &#9;&#9;&#9;&#9;&#9;&#9;VStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;// Axis name and distribution works &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack(alignment: .lastTextBaseline) { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;TextField("", text: $axis.name) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.controlSize(.small) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.font(.headline) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;DistributionView( &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;value: $axis.distribution) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;.controlSize(.mini) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;// It causes compiler `Cannot assign to property: 'lowerBound' is a 'let' constant`. But it isn't. ? &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;HStack { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ValueView(value: $axis.bounds.lowerBound) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;ValueView(value: $axis.bounds.upperBound) &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;... When axis.bounds.lowerbound and axis.bounds.upperbound became let? How to edit them in View?
1
0
2.2k
Sep ’20