Search code examples
swiftswiftuixcplayground

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

Solution

  • It seems there is a bug with (at least this version) of Playground, when using ForEach. I have the same issue and you can find more details in the CrashLogs of console

    Check crashing playground with ForEach

    Workarround

    • Move ContentView to a separate file in 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)
        }
    }