Home > Mobile >  Is there a way to pass a KeyPathComparator in Swift/SwiftUI into another function?
Is there a way to pass a KeyPathComparator in Swift/SwiftUI into another function?

Time:11-26

I am sorting using the new nifty Table in SwiftUI 3.0. However, I need to do a lot more filtering on the data before returning it.

Here's what I have (specific question is in the .onChange closure)

@State private var sorting = [KeyPathComparator(\Variant.id)]

...other stuff

            Table(displayvariants, selection: $selection, sortOrder: $sorting) {
                TableColumn("Variant ID", value: \.id) {
                    Text("\($0.id)")
                }
                TableColumn("Chromosome", value: \.id) {
                    Text("\($0.chromosome)")
                }
                TableColumn("Read Depth", value: \.read_depth) {
                    Text("\($0.read_depth)")
                }
                TableColumn("Ref Allele", value: \.reference_allele!)
                TableColumn("Var Allele", value: \.variant_allele!)
            }
            .onChange(of: sorting) {
                displayvariants.sort(using: $0)
            }

Here's what I would like to have (see change in .onChange closure)

@State private var sorting = [KeyPathComparator(\Variant.id)]

..... important stuff

            Table(displayvariants, selection: $selection, sortOrder: $sorting) {
                TableColumn("Variant ID", value: \.id) {
                    Text("\($0.id)")
                }
                TableColumn("Chromosome", value: \.id) {
                    Text("\($0.chromosome)")
                }
                TableColumn("Read Depth", value: \.read_depth) {
                    Text("\($0.read_depth)")
                }
                TableColumn("Ref Allele", value: \.reference_allele!)
                TableColumn("Var Allele", value: \.variant_allele!)
            }
            .onChange(of: sorting) {
                displayvariants = complexSortAndFilterFunction(KeyPathComparator: sortDescription)
            }

Is this even possible? If I need to change my approach I'd be happy to hear about it.

CodePudding user response:

you could do something simple like this:

Table(displayvariants, selection: $selection , sortOrder: $sorting) {
    TableColumn("Variant ID", value: \.id) {
        Text("\($0.id)")
    }
    TableColumn("Chromosome", value: \.id) {
        Text("\($0.chromosome)")
    }
    TableColumn("Read Depth", value: \.read_depth) {
        Text("\($0.read_depth)")
    }
    TableColumn("Ref Allele", value: \.reference_allele!)
    TableColumn("Var Allele", value: \.variant_allele!)
}
.onChange(of: sorting) { sortDescription in   // <--- here
      displayvariants = complexSortAndFilterFunction(KeyPathComparator: sortDescription)
}



func complexSortAndFilterFunction(KeyPathComparator: [KeyPathComparator<Variant>]) -> [Variant] {
     // ... do a lot more filtering on the data before returning it
     return [Variant]  // <-- return the results
}
  • Related