Aim:
I have a model which is an
ObservableObject
. It has a Bool
property, I would like to use this Bool
property to initialise a @Binding
variable.Questions:
- How to convert an
to a@ObservableObject
?@Binding
- Is creating a
the only way to initialise a@State
?@Binding
Note:
- I do understand I can make use of
/@ObservedObject
, and I see it's usefulness, but I am not sure a simple button needs to have access to the entire model.@EnvironmentObject
- Or is my understanding incorrect ?
Code:
import SwiftUI
import Combine
import SwiftUI
import PlaygroundSupport
class Car : ObservableObject {
@Published var isReadyForSale = true
}
struct SaleButton : View {
@Binding var isOn : Bool
var body: some View {
Button(action: {
self.isOn.toggle()
}) {
Text(isOn ? "On" : "Off")
}
}
}
let car = Car()
//How to convert an ObservableObject to a Binding
//Is creating an ObservedObject or EnvironmentObject the only way to handle a Observable Object ?
let button = SaleButton(isOn: car.isReadyForSale) //Throws a compilation error and rightly so, but how to pass it as a Binding variable ?
PlaygroundPage.current.setLiveView(button)
ObservableObject will provide you with a binding to any contained property automatically via the $-prefix syntax:
class Car: ObservableObject {
@Published var isReadyForSale = true
}
struct SaleView: View {
@Binding var isOn: Bool
var body: some View {
Toggle("Ready for Sale", isOn: $isOn)
}
}
struct ContentView: View {
@ObservedObject var car: Car
var body: some View {
Text("Details")
.font(.headline)
SaleView(isOn: $car.isReadyForSale) // generates a Binding to 'isReadyForSale' property
}
}