swiftdata-pro

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese
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.
编写并审核SwiftData代码,确保代码正确、使用现代API且符合项目规范。仅报告真实存在的问题——不要吹毛求疵或捏造问题。
审核流程:
  1. 参考
    references/core-rules.md
    检查核心SwiftData问题。
  2. 参考
    references/predicates.md
    检查谓词是否安全且受支持。
  3. 如果项目使用CloudKit,参考
    references/cloudkit.md
    检查CloudKit特定约束。
  4. 如果项目目标为iOS 18+,参考
    references/indexing.md
    检查索引优化机会。
  5. 如果项目目标为iOS 26+,参考
    references/class-inheritance.md
    检查类继承模式。
如果仅处理部分工作,只需加载相关的参考文件。

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.
  • 目标为Swift 6.2或更高版本,使用现代Swift并发特性。
  • 用户强烈偏好全面使用SwiftData。除非是SwiftData无法解决的功能需求,否则不要推荐Core Data相关功能。
  • 未经询问,不要引入第三方框架。
  • 使用一致的项目结构,根据应用功能确定文件夹布局。

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:
如果用户要求审核代码,按文件整理发现的问题。每个问题需包含:
  1. 说明文件及相关行号。
  2. 指出违反的规则名称。
  3. 展示简短的修改前后代码对比。
跳过没有问题的文件。最后按优先级总结最需优先处理的关键修改。
如果用户要求编写或优化代码,遵循上述规则直接修改代码,而非返回问题报告。
示例输出:

Destination.swift

Destination.swift

Line 8: Add an explicit delete rule for relationships.
swift
// Before
var 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 }
第8行:为关系添加显式删除规则。
swift
// Before
var sights: [Sight]

// After
@Relationship(deleteRule: .cascade, inverse: \Sight.destination) var sights: [Sight]
第22行:不要在谓词中使用
isEmpty == false
——这会导致运行时崩溃。请改用
!
swift
// Before
#Predicate<Destination> { $0.sights.isEmpty == false }

// After
#Predicate<Destination> { !$0.sights.isEmpty }

DestinationListView.swift

DestinationListView.swift

Line 5:
@Query
must only be used inside SwiftUI views.
swift
// Before
class DestinationStore {
    @Query var destinations: [Destination]
}

// After
class DestinationStore {
    var modelContext: ModelContext

    func fetchDestinations() throws -> [Destination] {
        try modelContext.fetch(FetchDescriptor<Destination>())
    }
}
第5行:
@Query
仅可在SwiftUI视图内使用。
swift
// Before
class DestinationStore {
    @Query var destinations: [Destination]
}

// After
class 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.
  1. 数据丢失(高优先级): Destination.swift第8行缺少删除规则,意味着删除目的地时,关联的景点会成为孤立数据。
  2. 崩溃(高优先级): 第22行的
    isEmpty == false
    会导致运行时崩溃——请改用
    !isEmpty
  3. 行为异常(高优先级): DestinationListView.swift第5行的
    @Query
    仅在SwiftUI视图内有效。
示例结束。

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.
  • references/core-rules.md
    - 自动保存、关系管理、删除规则、属性限制及FetchDescriptor优化。
  • references/predicates.md
    - 受支持的谓词操作、会导致运行时崩溃的危险模式及不支持的方法。
  • references/cloudkit.md
    - CloudKit特定约束,包括唯一性、可选性和最终一致性。
  • references/indexing.md
    - iOS 18+的数据库索引,包括单属性索引和复合属性索引。
  • references/class-inheritance.md
    - iOS 26+的模型子类化,包括@available要求、架构设置及谓词过滤。