I have a UInt? (optional) property in my data model that I am trying to bind to a Toggle and Slider with SwiftUI. I am trying to get to something like this:
maximumRingsShownCounthave value 4 (not nil), then the toggle should be on and the value bound to the slider.maximumExpandedRingsShownCountvalue is nil, then the toggle should off and the slider is not shown.
I am facing 2 issues here:
- It looks like we cannot have optional bindings (for the Slider)
- Is it possible to have a transformer to transform the optional to boolean (for the Toggle)?
So far, in my view I declared 2 properties in addition to my model:
@ObjectBinding var configuration: SunburstConfiguration
@State private var haveMaximumRingsShownCount: Bool = false
@State private var haveMaximumExpandedRingsShownCount: Bool = false
And my view body contains (for each property):
Toggle(isOn: $haveMaximumRingsShownCount) {
Text("maximumRingsShownCount")
}
if haveMaximumRingsShownCount {
VStack(alignment: .leading) {
Text("maximumRingsShownCount = \(config.maximumRingsShownCount!)")
Slider(value: .constant(4 /* no binding here :( */ ))
}
}
}
The UI layout is correct but I still have the issues mentioned above because:
- The
haveMaximumRingsShownCountstate is not bound to myconfig.maximumRingsShownCountmodel being nil or not - The slider is currently just displaying a constant and not bound to the unwrapped
config.maximumRingsShownCountproperty
Any ideas on how to solve these issue with optionals?
[ This can be reproduced in the sample code at https://github.com/lludo/SwiftSunburstDiagram ]
