gpt4 book ai didi

swift - 如何将通用 View 传递给 SwiftUI 中的结构

转载 作者:行者123 更新时间:2023-12-04 08:47:01 24 4
gpt4 key购买 nike

我收到错误 Failed to produce diagnostic for expression; please file a bug report对于以下代码。在我看来,这很直接,但可能与泛型有关。如何将通用 View 传递给结构?
在 SwiftUI View 中,我有这个:

import SwiftUI

struct PageView<Page: View>: View {
var views: [UIHostingController<Page>]

init(_ views: [Page]) {
self.views = views.map { UIHostingController(rootView: $0) }
}

var body: some View {
Text("Page View")
}
}

struct View1: View {
var body: some View {
Text("View 1")
}
}

struct View2: View {
var body: some View {
Text("View 2")
}
}

struct Reference: View {
var body: some View { // <- Error here
PageView([View1(), View2()])
}
}

最佳答案

这是可能的解决方案

struct Reference: View {
var body: some View {
PageView([AnyView(View1()), AnyView(View2())])
}
}

关于swift - 如何将通用 View 传递给 SwiftUI 中的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64262265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com