I am trying to observe changes of a bool
value contained in an ObservableObject
which is a value in an enum
case. Here is an example of what I am trying to achieve but with the current approach I receive the error Use of unresolved identifier '$type1Value'
.
import SwiftUI
import Combine
class ObservableType1: ObservableObject {
@Published var isChecked: Bool = false
}
enum CustomEnum {
case option1(ObservableType1)
}
struct Parent: View {
var myCustomEnum: CustomEnum
var body: AnyView {
switch myCustomEnum {
case .option1(let type1Value):
AnyView(Child(isChecked: $type1Value.isChecked)) // <- error here
}
}
}
struct Child: View {
@Binding var isChecked: Bool
var body: AnyView {
AnyView(
Image(systemName: isChecked ? "checkmark.square" : "square")
.onTapGesture {
self.isChecked = !self.isChecked
})
}
}
I am trying to update the value of isChecked
from the interface but since I want to have the ObservableObject
which contains the property in an enum
like CustomEnum
not sure how to do it or if it is even possible. I went for an enum because there will be multiple enum options with different ObservableObject
values and the Parent
will generate different subviews depending on the CustomEnum
option. If it makes any relevance the Parent
will receive the myCustomEnum
value from an Array
of CustomEnum
values. Is this even possible? If not, what alternatives do I have? Thank you!