I have a viewmodel class like so:
class SharedViewModel: ObservableObject {
@Published var objectHeight: Int? = nil
}
and I am trying to bind to bind the variable to a textfield with a number formatter like so (the variable needs to be used throughout so in the current view the viewmodel is an @EnvironmentObject
):
TextField("Height", value: $sharedViewModel.$objectHeight, formatter: NumberFormatter()
But I get this error when I hit the enter key in the simulator: Could not cast value of type '__NSCFNumber' (0x7fd9d6830fa8) to 'Combine.Published<Swift.Optional<Swift.Int>>.Publisher' (0x7fd9d5197d10).
Edit: Using an optional Int as just a state variable works fine it is the Published tag while using an Observable object as an EnvironmentObject that seems to be causing the issue. However, I need this variable inside the EnvironmentObject as it is used in multiple views.