我目前有一个协调员,它引用三个不同的SwiftUI UIHostingController,它们是连续的,所以永远不会有两个同时活动。因此,我想我可以减少引用的数量,只保留一个泛型引用。
我的代码如下所示:
final class Coordinator {
private var currentIntroViewHost: UIHostingController<AnimationIntroView>?
private var currentNoValidTicketsViewHost: UIHostingController<NoValidTicketsView>?
private var currentErrorViewHost: UIHostingController<ErrorViewSwiftUI>?
}我想要实现的是:
final class Coordinator {
private var currentViewHost: UIHostingController<View>?
}然而,使用所有这三个视图都符合的协议似乎不起作用。我试过这样做:
protocol GenericView: SwiftUI.View {}
final class Coordinator {
private var currentViewHost: UIHostingController<GenericView>?
}我收到编译器错误:
类型“任意GenericView”不能符合“视图”
有办法完成我想做的事吗?
发布于 2022-10-03 10:42:36
正如@ScottM在评论中指出的那样,我能够利用UIHostingController是UIViewController的子类这一优点。在我的示例中,协调员没有必要知道当前的视图是UIHostingController类型的。
我的解决方案是这样的:
final class Coordinator {
private var currentViewController: UIViewController?
}https://stackoverflow.com/questions/73933029
复制相似问题