Swiftdata Pro

twostraws/swiftdata-agent-skill/swiftdata-pro

作者 twostraws922d989473a9MIT434 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫7 個月前更新

Writes, reviews, and improves SwiftData code using modern APIs and best practices. Use when reading, writing, or reviewing projects that use SwiftData.

AI 產生的概覽

審查並撰寫 SwiftData 程式碼,著重正確性、現代 API 用法與專案慣例。

功能
此技能引導代理審查或撰寫 Apple 平台的 SwiftData 程式碼。它會檢查核心規則、述詞安全性、CloudKit 限制、iOS 18+ 的索引,以及 iOS 26+ 的類別繼承,並在部分工作中只載入相關參考檔案。審查結果依檔案整理,包含行號、違反的規則與修改前後的程式碼範例,最後提供依優先順序排列的摘要;若要求撰寫或改進程式碼,則直接進行修改。
適用情境
適用於閱讀、撰寫或審查使用 SwiftData 的專案,尤其是排查執行階段崩潰、資料遺失風險或過時的 API 用法。也適合針對述詞安全性、CloudKit 相容性、索引或模型子類別化等專項檢查。
執行需求
不需要指令碼,僅包含指示與參考 Markdown 檔案。假定使用 Swift 6.2 或更新版本及現代 Swift 並行,面向 SwiftData 專案,可選涉及 CloudKit 或特定 iOS 版本。

Write and review SwiftData code for correctness, modern API usage, and adherence to project conventions. Report only genuine problems - do not nitpick or invent issues.

Review process:

  1. Check for core SwiftData issues using references/core-rules.md.
  2. Check that predicates are safe and supported using references/predicates.md.
  3. If the project uses CloudKit, check for CloudKit-specific constraints using references/cloudkit.md.
  4. If the project targets iOS 18+, check for indexing opportunities using references/indexing.md.
  5. If the project targets iOS 26+, check for class inheritance patterns using references/class-inheritance.md.

If doing partial work, load only the relevant reference files.

Core Instructions

  • Target Swift 6.2 or later, using modern Swift concurrency.
  • The user strongly prefers to use SwiftData across the board. Do not suggest Core Data functionality unless it is a feature that cannot be solved with SwiftData.
  • Do not introduce third-party frameworks without asking first.
  • Use a consistent project structure, with folder layout determined by app features.

Output Format

If the user asks for a review, organize findings by file. For each issue:

  1. State the file and relevant line(s).
  2. Name the rule being violated.
  3. Show a brief before/after code fix.

Skip files with no issues. End with a prioritized summary of the most impactful changes to make first.

If the user asks you to write or improve code, follow the same rules above but make the changes directly instead of returning a findings report.

Example output:

Destination.swift

Line 8: Add an explicit delete rule for relationships.

swift
// Beforevar sights: [Sight]
// After@Relationship(deleteRule: .cascade, inverse: \Sight.destination) var sights: [Sight]

Line 22: Do not use isEmpty == false in predicates – it crashes at runtime. Use ! instead.

swift
// Before#Predicate<Destination> { $0.sights.isEmpty == false }
// After#Predicate<Destination> { !$0.sights.isEmpty }

DestinationListView.swift

Line 5: @Query must only be used inside SwiftUI views.

swift
// Beforeclass DestinationStore {    @Query var destinations: [Destination]}
// Afterclass DestinationStore {    var modelContext: ModelContext
    func fetchDestinations() throws -> [Destination] {        try modelContext.fetch(FetchDescriptor<Destination>())    }}

Summary

  1. Data loss (high): Missing delete rule on line 8 of Destination.swift means sights will be orphaned when a destination is deleted.
  2. Crash (high): isEmpty == false on line 22 will crash at runtime – use !isEmpty instead.
  3. Incorrect behavior (high): @Query on line 5 of DestinationListView.swift only works inside SwiftUI views.

End of example.

References

  • references/core-rules.md - autosaving, relationships, delete rules, property restrictions, and FetchDescriptor optimization.
  • references/predicates.md - supported predicate operations, dangerous patterns that crash at runtime, and unsupported methods.
  • references/cloudkit.md - CloudKit-specific constraints including uniqueness, optionality, and eventual consistency.
  • references/indexing.md - database indexing for iOS 18+, including single and compound property indexes.
  • references/class-inheritance.md - model subclassing for iOS 26+, including @available requirements, schema setup, and predicate filtering.

來源與署名

來源:twostraws/swiftdata-agent-skill位於swiftdata-pro提交922d989

授權條款: MIT

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架