I'd like to extend ObservableObject behavior in SwiftUI to nested classes, and I'm looking for the proper way to do it. It can be done "manually" with Combine, but I imagine there's a much cleaner way to do it using SwiftUI, and I'm hoping you can point me in the right direction. Here's what I mean…
Below is a typical application of ObservableObject to make a View dynamically respond to changes to a reference type. Tapping the button toggles the showText
value, which makes the text appear/disappear on the screen:
import SwiftUI
class MyClass: ObservableObject {
@Published var showText = false
}
struct ContentView: View {
@ObservedObject var instance = MyClass()
var body: some View {
VStack(spacing: 10) {
Button(action: {
print(self.instance.showText)
self.instance.showText.toggle()
}) {
Text("BUTTON").bold().padding()
.foregroundColor(.white)
.background(Color.red)
}
if instance.showText {
Text("Hello, World!")
}
}
}
}
This works fine.
But what about the modification below, where the class containing showText
is an InnerClass
, itself contained in an OuterClass
? The button toggles showText
just fine, but the notification of the value change no longer propagates through the OuterClass
instance to the View, so the View no longer displays the Text at all.
import SwiftUI
class OuterClass: ObservableObject {
@Published var innerInstance = InnerClass()
}
class InnerClass: ObservableObject {
@Published var showText = false
}
struct ContentView: View {
@ObservedObject var outerInstance = OuterClass()
var body: some View {
VStack(spacing: 10) {
Button(action: {
self.outerInstance.innerInstance.showText.toggle()
}) {
Text("BUTTON").bold().padding()
.foregroundColor(.white)
.background(Color.red)
}
if outerInstance.innerInstance.showText {
Text("Hello, World!")
}
}
}
}
What is the elegant fix for this?