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 を Playground のソース内の別のファイルに移動します
  • public修飾子を忘れないでください

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 に答える