I’d like to know if there is a good existing way to wrap a UIKit inside SwiftUI when the initializer of the UIKit component can throw an error.
As a context, I’m using this article to create a generic wrapper like this:
struct Wrap<Wrapped: UIView>: UIViewRepresentable {
typealias Updater = (Wrapped, Context) -> Void
var makeView: () -> Wrapped
var update: (Wrapped, Context) -> Void
init(_ makeView: @escaping @autoclosure () -> Wrapped,
updater update: @escaping Updater) {
self.makeView = makeView
self.update = update
}
func makeUIView(context: Context) -> Wrapped {
makeView()
}
func updateUIView(_ view: Wrapped, context: Context) {
update(view, context)
}
}
extension Wrap {
init(_ makeView: @escaping @autoclosure () -> Wrapped,
updater update: @escaping (Wrapped) -> Void) {
self.makeView = makeView
self.update = { view, _ in update(view) }
}
init(_ makeView: @escaping @autoclosure () -> Wrapped) {
self.makeView = makeView
self.update = { _, _ in }
}
}
Now, the problem is that, if the UIKit class has an initializer that can throw an error, attempting to use it in a SwiftUI screen will not compile:
final class MyView: UIView {
init(text: String) throws {
throw NSError(domain: NSOSStatusErrorDomain, code: 20)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
So what would be the best way to handle this kind of situation in a “SwiftUI” way?