繁体   English   中英

SwiftUI 和 foreach 在 Xcode 操场上的问题

[英]Problem with SwiftUI and foreach on Xcode playground

我正在尝试在 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)
    }
}

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

使用 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)
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM