Goals

✅ Custom bindings for editing form

Progress

In his article about custom binding in SwiftUI, Paul Hudson shows that we can create an instance of class Binding (which is what is generated when we use the @State property wrapper) to customise how the binding of a @State variable is handled.

But I found that if I set an ObservableObject with regular get and set modifiers in a computed variable, the binding generated by @Observed would work just as I expected.

So now I’m bit confused of what’s the difference between using a custom Binding<> object compared to the regular computed variable feature I would have from the mentioned approach.

(Looks like a good blog topic to explore).

(Currently editing… ⏱)

In fact if I use get and set the values aren’t propagated to the Combine objects that SwiftUI uses. It means that although the internal property is changed, the binding doesn’t reflect what is passed to a TextField for example.

And there are some discussions in StackOverflow about how to work with formatters and SwiftUI, and how to create custom bindings as well.

(Currently editing… ⏱)

By the way, the Picker view must receive an Int to its selection: argument.

Conclusion

I was able to create a view model EntryFormViewModel that intermediates the changes to ExpenseEntry and formats its values accordingly.

The next task is to save the changes to the object it has acquired and pass it back to the list. I think I will have to change the struct to be a class so I can update the reference, or at least I would have to use @Binding somehow to do that.

Yearvu source code can be found in my GitHub.

Check out the commit in the public repository.