<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftData",
  "identifier" : "/documentation/SwiftData/Query/sections",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Property",
  "symbol" : {
    "kind" : "Instance Property",
    "modules" : [
      "SwiftData",
      "SwiftUI",
      "_SwiftData_SwiftUI"
    ],
    "preciseIdentifier" : "s:011_SwiftData_A2UI5QueryV8sections0aB016SectionedResultsVyxSSGvp"
  },
  "title" : "sections"
}
-->

# sections

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

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

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)