简体   繁体   English

SwiftUI 和 foreach 在 Xcode 操场上的问题

[英]Problem with SwiftUI and foreach on Xcode playground

i am trying to execute a simple code in SwiftUI but it shows error: Execution was interrupted, reason: signal SIGABRT.我正在尝试在 SwiftUI 中执行一个简单的代码,但它显示错误:执行被中断,原因:信号 SIGABRT。 here is a code `这是一个代码`

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

It seems there is a bug with (at least this version) of Playground, when using ForEach.使用 ForEach 时,(至少这个版本)的 Playground 似乎存在一个错误。 I have the same issue and you can find more details in the CrashLogs of console我有同样的问题,您可以在console的 CrashLogs 中找到更多详细信息

Check crashing playground with ForEach 使用 ForEach 检查崩溃的游乐场

Workarround解决方法

  • Move ContentView to a separate file in Sources of Playground将 ContentView 移动到 Sources of Playground 中的单独文件
  • Don't forget the public modifiers不要忘记公共修饰符

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