sections
The sections computed from the current results, grouped by the sectionBy key path.
Declaration
@MainActor @preconcurrency var sections: SectionedResults<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. For SectionedResults-typed queries, access sections through the property directly; for [Element]-typed queries, use the underscore-prefix accessor:
// Preferred — SectionedResults result type
@Query(sort: \.name, sectionBy: \.category)
var items: SectionedResults<Item, String>
var body: some View {
List {
ForEach(items) { section in
Section(section.title) {
ForEach(section) { item in Text(item.name) }
}
}
}
}