簡體   English   中英

啟動具有不同視圖一致性的 UIHostingViewController 時,類型“任何視圖”不能符合“視圖”

[英]Type 'any View' cannot conform to 'View' when initiating UIHostingViewController with different View conformances

我真的很喜歡 Swift 類型系統。 但我無法理解為什么我不能這樣做:

        var view: View
        if #available(tvOS 15.0, *) {
            view = ManageProfilesView.MainView(
                    viewModel: .init(),
                    action: { _ in }
            )
        } else {
            view = ManageProfilesView.LegacyView(
                    viewModel: .init(),
                    action: { _ in }
                )
        }
        let viewController = UIHostingController(rootView: view) // Type 'any View' cannot conform to 'View'
        presenter.show(viewController, sender: presenter)

很明顯,這兩個視圖都是 View 協議的正確一致性,所以let view: View應該滿足UIHostingController<Content>: UIViewController where Content: View的要求

View 是具有關聯類型的協議。 這意味着存在無限數量的 View 協議,每個協議都有自己的關聯類型。 您可以想象,在 true 條件下,您將 int 分配給 view 變量,而在 false 條件下,您嘗試分配 string 變量。

您只需要創建第三個視圖並將該條件放入它的主體中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM