Why state doesn't change when I use class for state instead of struct. #417
Replies: 2 comments 6 replies
-
Even if some fields of your class are changing, the class instance probably doesn't. If let newValue = TheClass()
newValue.text = "Hello"
state.theClass = newValue the change should be detected as you're setting a new reference to let newValue = MutatingTheClass(state.theClass)
newValue.text = "Hello"
state.theClass = newValue You can define a computed property on struct State {
private var _theClass: TheClass = .init()
public var theClass: MutableTheClass {
get {
MutableTheClass(self._theClass)
}
set {
_theClass = newValue
}
}
} This should work as you're never able to get Otherwise, you can directly publicly expose Good luck! |
Beta Was this translation helpful? Give feedback.
-
Value types are the natural choice for modelling your state - can I ask why you're trying to use a reference type? Is this to try and overcome some kind of performance concern by moving your state off the stack and on to the heap? |
Beta Was this translation helpful? Give feedback.
-
Hello Friends,
I come to this problem today and still not having an idea what makes things not triggered when class used instead of struct for the case when I need reference type instead of value type, I implemented Equatable for this class.
is still something missing for me ?
Beta Was this translation helpful? Give feedback.
All reactions