Cannot assign to property: 'lowerBound' is a 'let' constant — becomes 'let' in UIView

I have a struct:
Code Block
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:
Code Block
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:
Code Block
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?

When axis.bounds.lowerbound and axis.bounds.upperbound became let?

Please check the doc of ClosedRange. lowerBound and upperBound are let-constants.

How to edit them in View?

Replace the whole bounds. You can prepare this extension:
Code Block
extension SpaceAxisProtocol {
var lowerBound: CoordUnit {
get {bounds.lowerBound}
set {
bounds = newValue...bounds.upperBound
}
}
var upperBound: CoordUnit {
get {bounds.upperBound}
set {
bounds = bounds.lowerBound...newValue
}
}
}

And use it like this:
Code Block
struct StyledAxisView<SA:StyledAxisProtocol>: View {
@Binding var axis: SA
public var body : some View {
VStack {
//...
HStack {
ValueView(value: $axis.lowerBound) //<-
ValueView(value: $axis.upperBound) //<-
}
//...
}
}
}


Cannot assign to property: 'lowerBound' is a 'let' constant — becomes 'let' in UIView
 
 
Q