在SwiftUI的MacOS表格中的部分无法构建

huangapple go评论58阅读模式
英文:

Sections in a SwiftUI MacOS Table will not build

问题

在这段代码中,编译器建议你将表达式分解为明确的子表达式以便更好地进行类型检查。你可以尝试将 theTable 这个视图的构建过程拆分为多个步骤,以减轻编译器的负担。

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

通过将 theTable 拆分为多个子表达式,你可以更好地帮助编译器理解代码的结构,从而提高类型检查的效率。

英文:

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?

答案1

得分: 0

我在另一个帖子中找到了解决方案:

macOS SwiftUI 表格超过10列?

所以我在这一行中添加了 as Flight

Table(groupedFlights[key]! as [Flight]){
英文:

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]){

huangapple
  • 本文由 发表于 2023年3月31日 02:34:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75891817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定