n0an/VivaDicta / .agents/skills/swiftdata-pro
swiftdata-pro
VerifiedWrites, reviews, and improves SwiftData code using modern APIs and best practices. Use when reading, writing, or reviewing projects that use SwiftData.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.6 KB |
| agents/openai.yaml | config | 340 B |
| assets/swiftdata-pro-icon.png | binary | 3.0 KB |
| assets/swiftdata-pro-icon.svg | other | 1.5 KB |
| references/class-inheritance.md | markdown | 3.7 KB |
| references/cloudkit.md | markdown | 662 B |
| references/core-rules.md | markdown | 3.9 KB |
| references/indexing.md | markdown | 800 B |
| references/predicates.md | markdown | 2.4 KB |
references/predicates.md
View on GitHub# Working with predicates
SwiftData predicates support only a subset of Swift functionality. Some things are marked as being unsupported, meaning that they will not build. Other things are *not* marked as unsupported and yet are still not supported, meaning that they will build but crash at runtime.
This guide contains specific guidance on what to use and when.
## String matching
When writing a query predicate to perform string matching, always use `localizedStandardContains()` rather than trying to use `lowercased().contains()` or similar.
For example, this is correct:
```swift
@Query(filter: #Predicate<Movie> {
$0.name.localizedStandardContains("titanic")
}) private var movies: [Movie]
```
## hasPrefix()
`hasPrefix()` and `hasSuffix()` are not supported in SwiftData predicates. If you want to use `hasPrefix()`, you should use `starts(with:)` instead, like this:
```swift
@Query(filter: #Predicate<Website> {
$0.type.starts(with: "https://apple.com")
}) private var appleLinks: [Website]
```
## Unsupported predicates
Many common methods have no equivalent in SwiftData, and will not compile. For example, all these common operations are not supported:
- `String.hasSuffix()`
- `String.lowercased()`
- `Sequence.map()`
- `Sequence.reduce()`
- `Sequence.count(where:)`
- `Collection.first`
Custom operators are also not allowed.
## Dangerous predicates
Some SwiftData predicates will compile cleanly then fail or even crash at runtime.
For example, this is a valid predicate designed to show only movies that have a non-empty cast list:
```swift
@Query(filter: #Predicate<Movie> { !$0.cast.isEmpty }, sort: \Movie.name) private var movies: [Movie]
```
However, *this* query looks like it does the same thing, but will crash at runtime:
```swift
@Query(filter: #Predicate<Movie> { $0.cast.isEmpty == false }, sort: \Movie.name) private var movies: [Movie]
```
Never attempt to create query predicates that use computed properties, `@Transient` properties, or use custom `Codable` struct data. They might compile cleanly, but they will crash at runtime.
All predicates must rely on data that is actually stored in the database as `@Model` classes.
Never attempt to use regular expressions in predicates. They will compile cleanly then fail at runtime. So, this is *not* allowed:
```swift
@Query(filter: #Predicate<Movie> {
$0.name.contains(/Titanic/)
}, sort: \Movie.name)
private var movies: [Movie]
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| receiving-code-reviewobra/superpowersUse when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| requesting-code-reviewobra/superpowersUse when completing tasks, implementing major features, or before merging to verify work meets requirements | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| using-git-worktreesobra/superpowersUse when starting feature work that needs isolation from current workspace or before executing implementation plans - ensures an isolated workspace exists via native tools or git worktree fallback | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| verification-before-completionobra/superpowersUse when about to claim work is complete, fixed, or passing, before committing or creating PRs - requires running verification commands and confirming output before making any success claims; evidence before assertions always | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |