【问题标题】:Type() cannot conform to 'View' in Swift?Type() 不能符合 Swift 中的 \'View\'?
【发布时间】:2022-08-18 14:58:10
【问题描述】:

我想知道是否有人可以帮助解释为什么我可能会收到错误:

类型\'()\'不能符合\'View\'

我一直在查看其他一些与错误有类似问题的帖子,但我似乎仍然无法弄清楚。

这是我的代码供参考:

var body: some View {
    ScrollView (.vertical, showsIndicators: false) {
        VStack {
            HeaderComponent()
            Spacer(minLength: 10)
            
                if (!matchSet.mSet.isEmpty) {
                    VStack { //**this is where the error is occurs**
                        matchSet.mSet.forEach { college in
                            CollegeButton(name: college.dest.name)
                        }

这里也是 CollegeButton 结构

struct CollegeButton: View {
var name: String

var body: some View {
    Button(action: {
        print(\"You clicked \" + name)
    }) {
        Text(name.uppercased())
            .modifier(ButtonModifier())
    }

}

}

标签: swift swiftui


【解决方案1】:

你不要在SwiftUI 中使用.forEach,而是试试这个:

ForEach(matchSet.mSet) { college in
    CollegeButton(name: college.dest.name)
}

主要区别在于ForEachView,而.forEachVoid 类型。只需确保 mSet 符合 Identifiable

【讨论】:

    猜你喜欢
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多