Guide SwiftUI development for macOS apps. Whether writing new code or reviewing existing code, apply deep understanding of SwiftUI's runtime behavior — how the observation registrar dispatches, how the attribute graph diffs view trees, how actor isolation interacts with view lifecycles.
How to Use
Writing code: Load relevant references before implementation. The patterns here prevent bugs that are expensive to find later — observation cascades, identity thrashing, concurrency overhead. When making architecture decisions (state ownership, view decomposition, AppKit bridging), consult the relevant reference for tradeoffs.
Reviewing code: Check each reference area systematically. Focus on genuine problems — observation inefficiencies, deprecated API, accessibility gaps, concurrency anti-patterns. Don't flag obvious issues or invent problems.
Partial scope: Load only what the task needs. Not every change requires all eight references.
References
| Reference | Load when |
|---|
| INLINECODE0 | INLINECODE1 , @State, ForEach, data-driven views, Observations {} streams |
| INLINECODE5 |
Task,
async/
await, actors,
DispatchQueue,
AsyncStream, scheduling decisions |
|
references/performance.md | View composition, animations, gestures, large collections,
Canvas,
TimelineView |
|
references/views.md | View decomposition, navigation,
.task(id:), preference keys, custom
Layout |
|
references/data.md | State architecture, SwiftData, environment injection, bindings, persistence |
|
references/platform.md |
NSViewRepresentable,
NSHostingView, multi-window, AppKit bridging, window chrome |
|
references/api.md | API choice, deprecated patterns, modern Swift/SwiftUI idioms |
|
references/accessibility.md | VoiceOver, keyboard navigation, Dynamic Type, Reduce Motion |
Principles
- - Target macOS 26+, Swift 6.2 with strict concurrency and
@MainActor default isolation. - Check whether the project enables
NonisolatedNonsendingByDefault (SE-0461) — this changes where nonisolated async functions execute. See references/concurrency.md. - Prefer SwiftUI-native solutions. Use AppKit (
NSViewRepresentable, NSHostingView, NSWindow) only when SwiftUI has no equivalent. - Understand the mechanism. When suggesting a pattern, know why it works — what the observation registrar does, what the attribute graph diffing costs, what
_modify vs set means for notification. Rules without understanding produce cargo-culted code. - Do not introduce third-party frameworks without asking. Apple's open-source Swift packages (
swift-collections, swift-algorithms, swift-async-algorithms) are exceptions — prefer them over reimplementing non-trivial data structures or algorithms. See references/api.md.
Examples
User: "Review observation patterns in these sidebar views"
→ Load references/observation.md. Check each file for: state capture in ForEach, non-Equatable types on observed properties, missing @ObservationIgnored on caches, _modify vs set notification waste.
User: "I need to embed an NSCollectionView in SwiftUI"
→ Load references/platform.md. Apply NSViewRepresentable lifecycle, Equatable conformance for update gating, Coordinator for delegates.
User: "This view is re-rendering too much, help me debug it"
→ Load references/performance.md and references/observation.md. Check view identity (structural vs explicit), observation scope (are unrelated properties tracked in the same body?), @State vs @Observable for gesture-driven values.
Review Output
Organize findings by file. For each issue:
- 1. File and relevant line(s).
- The pattern being violated and why it matters at the runtime level.
- Brief before/after code fix.
Skip clean files. End with a prioritized summary of the most impactful changes.
技能名称: swiftui-macos
详细描述:
指导macOS应用的SwiftUI开发。无论是编写新代码还是审查现有代码,都要深入理解SwiftUI的运行时行为——观察注册器如何调度、属性图如何对比视图树、Actor隔离如何与视图生命周期交互。
使用方法
编写代码:在实现前加载相关参考。这里的模式能防止日后代价高昂的bug——观察级联、标识抖动、并发开销。在做架构决策(状态所有权、视图分解、AppKit桥接)时,查阅相关参考以了解权衡。
审查代码:系统性地检查每个参考领域。聚焦于真正的问题——观察效率低下、已弃用API、无障碍缺口、并发反模式。不要标记明显问题或凭空制造问题。
局部范围:只加载任务所需的内容。并非每次修改都需要全部八个参考。
参考文档
| 参考文档 | 加载时机 |
|---|
| references/observation.md | @Observable、@State、ForEach、数据驱动视图、Observations {} 流 |
| references/concurrency.md |
Task、async/await、Actor、DispatchQueue、AsyncStream、调度决策 |
| references/performance.md | 视图组合、动画、手势、大型集合、Canvas、TimelineView |
| references/views.md | 视图分解、导航、.task(id:)、偏好键、自定义Layout |
| references/data.md | 状态架构、SwiftData、环境注入、绑定、持久化 |
| references/platform.md | NSViewRepresentable、NSHostingView、多窗口、AppKit桥接、窗口边框 |
| references/api.md | API选择、已弃用模式、现代Swift/SwiftUI惯用法 |
| references/accessibility.md | VoiceOver、键盘导航、动态字体、减少动态效果 |
原则
- - 目标为macOS 26+、Swift 6.2,启用严格并发和@MainActor默认隔离。
- 检查项目是否启用了NonisolatedNonsendingByDefault(SE-0461)——这会改变nonisolated异步函数的执行位置。参见references/concurrency.md。
- 优先使用SwiftUI原生解决方案。仅在SwiftUI没有等效方案时使用AppKit(NSViewRepresentable、NSHostingView、NSWindow)。
- 理解机制。在建议模式时,要知道为什么它有效——观察注册器做了什么、属性图对比的成本是多少、_modify与set对通知意味着什么。没有理解的规则只会产生照搬的代码。
- 未经询问不要引入第三方框架。苹果的开源Swift包(swift-collections、swift-algorithms、swift-async-algorithms)是例外——优先使用它们,而不是重新实现复杂的数据结构或算法。参见references/api.md。
示例
用户:审查这些侧边栏视图中的观察模式
→ 加载references/observation.md。检查每个文件:ForEach中的状态捕获、观察属性上的非Equatable类型、缓存上缺失的@ObservationIgnored、_modify与set的通知浪费。
用户:我需要在SwiftUI中嵌入一个NSCollectionView
→ 加载references/platform.md。应用NSViewRepresentable生命周期、用于更新门控的Equatable一致性、用于委托的Coordinator。
用户:这个视图重新渲染太频繁了,帮我调试
→ 加载references/performance.md和references/observation.md。检查视图标识(结构性与显式)、观察范围(不相关的属性是否在同一body中被追踪?)、用于手势驱动值的@State与@Observable。
审查输出
按文件组织发现。对于每个问题:
- 1. 文件和相关行号。
- 被违反的模式以及为什么它在运行时层面重要。
- 简要的修复前后代码对比。
跳过干净的文件。最后以影响最大的更改的优先级总结结束。