swiftdata-pro
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseWrite 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:
- Check for core SwiftData issues using .
references/core-rules.md - Check that predicates are safe and supported using .
references/predicates.md - If the project uses CloudKit, check for CloudKit-specific constraints using .
references/cloudkit.md - If the project targets iOS 18+, check for indexing opportunities using .
references/indexing.md - 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且符合项目规范。仅报告真实存在的问题——不要吹毛求疵或捏造问题。
审核流程:
- 参考检查核心SwiftData问题。
references/core-rules.md - 参考检查谓词是否安全且受支持。
references/predicates.md - 如果项目使用CloudKit,参考检查CloudKit特定约束。
references/cloudkit.md - 如果项目目标为iOS 18+,参考检查索引优化机会。
references/indexing.md - 如果项目目标为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:
- State the file and relevant line(s).
- Name the rule being violated.
- 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
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 in predicates – it crashes at runtime. Use instead.
isEmpty == false!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: must only be used inside SwiftUI views.
@Queryswift
// Before
class DestinationStore {
@Query var destinations: [Destination]
}
// After
class DestinationStore {
var modelContext: ModelContext
func fetchDestinations() throws -> [Destination] {
try modelContext.fetch(FetchDescriptor<Destination>())
}
}第5行:仅可在SwiftUI视图内使用。
@Queryswift
// Before
class DestinationStore {
@Query var destinations: [Destination]
}
// After
class DestinationStore {
var modelContext: ModelContext
func fetchDestinations() throws -> [Destination] {
try modelContext.fetch(FetchDescriptor<Destination>())
}
}Summary
总结
- Data loss (high): Missing delete rule on line 8 of Destination.swift means sights will be orphaned when a destination is deleted.
- Crash (high): on line 22 will crash at runtime – use
isEmpty == falseinstead.!isEmpty - Incorrect behavior (high): on line 5 of DestinationListView.swift only works inside SwiftUI views.
@Query
End of example.
- 数据丢失(高优先级): Destination.swift第8行缺少删除规则,意味着删除目的地时,关联的景点会成为孤立数据。
- 崩溃(高优先级): 第22行的会导致运行时崩溃——请改用
isEmpty == false。!isEmpty - 行为异常(高优先级): DestinationListView.swift第5行的仅在SwiftUI视图内有效。
@Query
示例结束。
References
参考文档
- - autosaving, relationships, delete rules, property restrictions, and FetchDescriptor optimization.
references/core-rules.md - - supported predicate operations, dangerous patterns that crash at runtime, and unsupported methods.
references/predicates.md - - CloudKit-specific constraints including uniqueness, optionality, and eventual consistency.
references/cloudkit.md - - database indexing for iOS 18+, including single and compound property indexes.
references/indexing.md - - model subclassing for iOS 26+, including @available requirements, schema setup, and predicate filtering.
references/class-inheritance.md
- - 自动保存、关系管理、删除规则、属性限制及FetchDescriptor优化。
references/core-rules.md - - 受支持的谓词操作、会导致运行时崩溃的危险模式及不支持的方法。
references/predicates.md - - CloudKit特定约束,包括唯一性、可选性和最终一致性。
references/cloudkit.md - - iOS 18+的数据库索引,包括单属性索引和复合属性索引。
references/indexing.md - - iOS 26+的模型子类化,包括@available要求、架构设置及谓词过滤。
references/class-inheritance.md