dpearson2699/swift-ios-skills / skills/swiftdata
swiftdata
VerifiedImplement, review, or improve data persistence using SwiftData. Use when defining @Model classes with @Attribute, @Relationship, @Transient, #Unique, or #Index; when querying with @Query, #Predicate, FetchDescriptor, or SortDescriptor; when configuring ModelContainer and ModelContext for SwiftUI or background work with @ModelActor; when planning schema migrations with VersionedSchema and SchemaMigrationPlan; when setting up CloudKit sync with ModelConfiguration; or when coexisting with or migrating from Core Data.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 15.4 KB |
| evals/evals.json | config | 3.8 KB |
| references/core-data-coexistence.md | markdown | 10.7 KB |
| references/indexing.md | markdown | 2.0 KB |
| references/predicate-pitfalls.md | markdown | 2.7 KB |
| references/swiftdata-advanced.md | markdown | 24.2 KB |
| references/swiftdata-queries.md | markdown | 18.0 KB |
references/predicate-pitfalls.md
View on GitHub# #Predicate Pitfalls
Common runtime crashes and build errors when using `#Predicate` with SwiftData.
## Unsupported Expressions
`#Predicate` compiles Swift expressions into a query representation. Only a subset of Swift is supported. Unsupported expressions compile but crash at runtime.
| Pattern | Crash? | Fix |
| --- | --- | --- |
| `$0.name.uppercased() == "PARIS"` | Runtime crash | Use `localizedStandardContains` or `caseInsensitiveCompare` |
| `$0.name.count > 5` | Runtime crash | Store length in a separate property or filter after fetch |
| `$0.tags.count == 0` | Runtime crash | Use `$0.tags.isEmpty` (iOS 17.4+) or a stored flag |
| `$0.name.isEmpty` on optional String | Runtime crash on some OS versions | Use `$0.name == nil \|\| $0.name == ""` |
| Custom computed property | Runtime crash | Only stored `@Attribute` properties work in predicates |
| `Date.now` captured by value | Stale predicate | Create a `let now = Date()` before the predicate and capture it |
| Enum raw value comparison | Runtime crash (pre-iOS 18) | Store the raw value as a separate property, or target iOS 18+ |
| Loops, declarations, mutation, or switch-heavy control flow | Build error or runtime failure | Use boolean logic, ternary expressions, optional chaining, or optional binding patterns supported by Foundation `Predicate` |
| Arbitrary method calls | Runtime crash | Only supported methods (see below) work |
## Supported Operations
**Comparisons:** `==`, `!=`, `<`, `<=`, `>`, `>=`
**Logic:** `&&`, `||`, `!`
**String:** `localizedStandardContains(_:)`, `contains(_:)`, `starts(with:)`, `caseInsensitiveCompare(_:)`
**Collections:** `contains(where:)`, `allSatisfy(_:)`, `filter(_:)`, `.isEmpty`
**Other:** optional chaining, nil coalescing (`??`), ternary (`? :`), arithmetic (`+`, `-`, `*`, `/`), type casting (`as?`, `is`)
## Safe Pattern: Build Predicates Dynamically
```swift
func tripPredicate(searchText: String, favoritesOnly: Bool) -> Predicate<Trip> {
let now = Date()
return #Predicate<Trip> { trip in
(searchText.isEmpty || trip.destination.localizedStandardContains(searchText))
&& (!favoritesOnly || trip.isFavorite)
&& trip.startDate > now
}
}
```
Capture all external values as `let` bindings outside the predicate closure. The predicate captures them by value at creation time.
## Debugging Predicate Crashes
When a predicate crashes at runtime with `SwiftData.PredicateError` or `NSInvalidArgumentException`:
1. Simplify the predicate to a single clause and add clauses back one at a time
2. Check each clause uses only supported operations on stored properties
3. Test on the minimum deployment target — some operations were added in later iOS versions
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| planning-and-task-breakdownaddyosmani/agent-skillsBreaks work into ordered tasks. Use when you have a spec or clear requirements and need to break work into implementable tasks. Use when a task feels too large to start, when you need to estimate scope, or when parallel work is possible. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| screenpipethedotmack/claude-memSearch screen and audio history recorded by Screenpipe and optionally save selected context into claude-mem. Use when the user asks to recall desktop activity, a meeting, or something seen in another app through Screenpipe. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |
| contact-widgetnexu-io/open-designSelf-contained floating chat widget with welcome screen, social links, meeting button, and message input. Single HTML file, zero dependencies. | Stars: 108k | Updated | Claude CodeCodex | Verified | Install |
| meeting-notesnexu-io/open-designMeeting notes page — title bar with attendees, agenda checklist, decisions block, action items table with owners + dates, and a "next meeting" footer. Use when the brief mentions "meeting notes", "minutes", "1:1 notes", "all-hands recap", or "会议纪要". | Stars: 108k | Updated | Claude CodeCodex | Verified | Install |