Search code examples
swiftuirealm

Sections in a SwiftUI MacOS Table will not build


I am trying to create a Table in SwiftUI for MacOS with sections in the table. The compiler wants me to break it up into sub-expressions, but I dont understand how to do this.

This is the error:

The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions

It happens on the ForEach here:

    var theTable: some View {
        ForEach(groupedFlights.keys.sorted()){ key in
            Text("DaySequenceNumber: \(key)")
            Table(groupedFlights[key]){
                TableColumn("FLIGHTNO", value: \.flightNo)
                TableColumn("DATE", value: \.date)
                TableColumn("FROM", value: \.from)
                TableColumn("TO", value: \.to)
                TableColumn("ACREG", value: \.acReg)
                TableColumn("STD", value: \.std)
                TableColumn("STA", value: \.sta)
                
            }
        }
    }

private var groupedFlights: [Int: [Flight]] {
        Dictionary(grouping: showingFlights, by: \.daySequenceNo)
    }

The Flight class:

import Foundation
import RealmSwift

class Flight: Object, ObjectKeyIdentifiable{
    @Persisted(primaryKey: true) var id:ObjectId
    @Persisted var flightNo:String
    @Persisted var date:String
    @Persisted var from:String
    @Persisted var to:String
    @Persisted var acReg:String
    @Persisted var std:String
    @Persisted var sta:String
    @Persisted var daySequenceNo:Int
    @Persisted var crewList:CrewList?
    
    var idString:String{
        id.description
    }
}

How can i break down theTable?


Solution

  • I found the solution in another thread:

    macOS SwiftUI Table with more than 10 columns?

    So I added as Flight to this line:

    Table(groupedFlights[key]! as [Flight]){