Contents

sections

The sections computed from the current results, grouped by the sectionBy key path.

Declaration

@MainActor @preconcurrency var sections: ResultsSectionCollection<Element, String> { get }

Discussion

Section names are String-typed. Both KeyPath<Element, String> and KeyPath<Element, String?> section keys produce String names — nil values map to the empty-string section.

Returns an empty collection when the query was not created with a sectionBy parameter. Access this from the query’s stored property using the underscore prefix:

struct ItemList: View {
    @Query(sort: \.category, sectionBy: \.category)
    var items: [Item]

    var body: some View {
        List {
            ForEach(_items.sections) { section in
                Section(section.name) {
                    ForEach(section) { item in Text(item.name) }
                }
            }
        }
    }
}

See Also

Accessing sections