首页 > 解决方案 > SwiftUI 中的静态项目列表

问题描述

我正在使用Xcode Version 12.3 (12C33)并且正在尝试添加带有项目的静态列表。我只能编译我的项目一次,它实际上工作得很好,现在我遇到了一些奇怪的错误。我在下面做了一个简单的代码示例,即使这样也给了我在其他项目中看到的相同错误。我错过了一些简单的东西吗?

struct ContentView: View {
    
    var body: some View {
        List {
            Text("Item 1")
            Text("Item 2")
            Text("Item 3")
        }
    }
}

Return type of property 'body' requires that 'List' conform to 'View'

Static method 'buildBlock' requires that 'List' conform to 'View'

Trailing closure passed to parameter of type Decoder that does not accept a closure.

标签: swiftswiftui

解决方案


在 SWIFTUI 中列出代码*

struct RestRow: View {
var name: String
var body: some View {
    Text("Restaurant: \(name)")
}
}

struct ContentView: View {
var body: some View {
    List {
        RestRow(name: "ITEM # 1")
        RestRow(name: "ITEM # 2")
        RestRow(name: "ITEM # 3")
    }
}
}

推荐阅读