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 从公开仓库中收录这些内容。

举报或申请下架