You should not initialise a @State member from within the initializer of a view, no matter how.
As discussed in Swift Forum, due to the internal implementation and due to how SwiftUI works, it seems initialising a @State is only safe in this form, outside any view initialiser:
struct MyView: View {
@State var value = 0
...
}
Note, that MyView value may be constructed multiple times in a single scene. This is in contrast to how UIKit works, where the views will be created only once for a scene.
When you initialise a @State within the view's initialiser, it will do it at the very first creation of this view, but it has no effect on subsequent initialisations. The SwiftUI framework will restore the previously cached value and thus overwrite the value you set in the initialiser.
Furthermore, mutating a @State variable is safe only within the result builder - that is, when it will be called directly or indirectly from body of the view - for example in onAppear.
So, in your case, if you want the variables from and to to be mutated by the view and being initialised by a view's initialiser use a binding (BindingT) instead.
If you don't need to modify the variables, just use plain let constants which can be initialised in the view's initializer.
See also:
Swift Forum - https://forums.swift.org/t/assignment-to-state-var-in-init-doesnt-do-anything-but-the-compiler-gened-one-works/35235/7
Swift Forum - https://forums.swift.org/t/state-messing-with-initializer-flow/25276
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: