3

我正在尝试在 SwiftUI 中执行一个简单的代码,但它显示错误:执行被中断,原因:信号 SIGABRT。这是一个代码`

struct ContentView: View {

    let data = (1...100).map { "Item \($0)" }

    let columns = [
        GridItem(.adaptive(minimum: 80))
    ]
    var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(data, id: \.self) { item in
                    Text(item)
                }
            }
            .padding(.horizontal)
        }
        .frame(maxHeight: 300)
    }
}
4

1 回答 1

6

使用 ForEach 时,(至少这个版本)的 Playground 似乎存在一个错误。我有同样的问题,您可以在 CrashLogs 中找到更多详细信息console

使用 ForEach检查崩溃的游乐场

解决方法

  • 将 ContentView 移动到 Sources of Playground 中的单独文件
  • 不要忘记公共修饰符

public struct ContentView: View {

    let data = (1...100).map { "Item \($0)" }

    let columns = [
        GridItem(.adaptive(minimum: 80))
    ]
    
    public init() {}
    
    public var body: some View {
        ScrollView {
            LazyVGrid(columns: columns, spacing: 20) {
                ForEach(data, id: \.self) { item in
                    Text(item)
                }
            }
            .padding(.horizontal)
        }
        .frame(maxHeight: 300)
    }
}
于 2021-04-16T07:34:50.870 回答