Where and how to set up binding converters in SwiftUI?

To convert between source and destination in a binding scenario there is a need for binding converters.


I used binding converters a lot in Cocoa applications, but now a couple of months within the beta-test program of SwiftUI, I have not figured out yet where they hide in the framework and how to set them up.


I hope that some can see, what I am seeing wrong or guide me to a solution to the problem.

Hi,


You need to use Operator, look at the doc here : https://developer.apple.com/documentation/combine/receiving_and_handling_events_with_combine


The sections :


Change the Output Type with Operators


and


Customize Publishers with Operators


And the WWDC talk : https://developer.apple.com/videos/play/wwdc2019/721


If you need more, I will take the time to build a simple example.


Regards


Gérard

Thanks for the answer.


I may have realized that my need for converters is not as high as first thought.


I used them a lot in Cocoa applications, but only seldom, I used two-way bindings and two-way converters.


In the concrete case, I was intending to (two-way) bind a Core Data property having a binary data type (NSAttributedString) to a SwiftUI text field. But this was not possible. Instead I will embed a UITextView into SwiftUI and add computed properties to my Core Data object that converts binary data to NSAttributed strings and vice versa.


Furthermore I now think that SwiftUI, because it is so fast and easy to construct new views, will make the need for converters less than previous.

Where and how to set up binding converters in SwiftUI?
 
 
Q