SKILL

swiftui-patterns

SwiftUI architecture patterns using @Observable for state management, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

Machine translation
Read the original Chinese text

SwiftUI 架构模式,使用 @Observable 进行状态管理,视图组合,导航,性能优化,以及现代 iOS/macOS UI 最佳实践。

Target: Mobile development. Automatically collected · unverified.

Excerpt from the author’s documentation

Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.

Read the original Chinese text

Topics in the source

  • SwiftUI 模式
  • 何时激活
  • 状态管理
  • 属性包装器选择
  • @Observable ViewModel
  • 消费 ViewModel 的视图
  • 环境注入
  • 视图组合

适用于 Apple 平台的现代 SwiftUI 模式,用于构建声明式、高性能的用户界面。涵盖 Observation 框架、视图组合、类型安全导航和性能优化。 构建 SwiftUI 视图和管理状态时(@State、@Observable、@Binding) 使用 NavigationStack 设计导航流程时 构建视图模型和数据流时 优化列表和复杂布局的渲染性能时 在 SwiftUI 中使用环境值和依赖注入时 选择最适合的最简单包装器: | 包装器 | 使用场景 | |---------|----------| | @State | 视图本地的值类型(开关、表单字段、Sheet 展示) | | @Binding | 指向父视图 @State 的双向引用 | | @Observable 类 + @State | 拥有多个属性的自有模型 | | @Observable 类(无包装器) | 从父视图传递的只读引用 | | @Bindable | 指向 @Observable 属性的双向绑定 | | @Environment | 通过 .environment() 注入的共享依赖项 | 使用 @Observable(而非 ObservableObject)—— 它跟踪属性级别的变更,因此 SwiftUI 只会重新渲染读取了已变更属性的视图: 用 @Environment 替换 @EnvironmentObject: 将视图拆分为小型、专注的结构体。当状态变更时,只有读取该状态的子视图会重新渲染: 使用 NavigationStack 与 NavigationPath 来实现程序化、类型安全的路由: LazyVStack 和 LazyHStack 仅在视图可见时才创建它们: 在 ForEach 中始终使用稳定、唯一的 ID —— 避免使用数组索引: 切勿在 body 内执行 I/O、网络调用或繁重计算 使用 .task {} 处理异步工作 —— 当视图消失时它会自动取消 在滚动视图中谨慎使用 .sensoryFeedback() 和 .geometryGroup() 在列表中最小化使用 .shadow()、.blur() 和 .mask() —— 它们会触发屏幕外渲染 对于 body 计算昂贵的视图,遵循 Equatable 以跳过不必要的重新渲染: 使用 #Preview 宏配合内联模拟数据以进行快速迭代: 在新代码中使用 ObservableObject / @Published / @StateObject / @EnvironmentObject —— 迁移到 @Observable 将异步工作直接放在 body 或 init 中 —— 使用 .task {} 或显式的加载方法 在不拥有数据的子视图中将视图模型创建为 @State —— 改为从父视图传递 使用

This is an excerpt. Refer to the complete source and its current requirements.

Source repository
affaan-m/ECC
Source file
docs/zh-CN/skills/swiftui-patterns/SKILL.md
Fixed revision
d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Read the complete original documentation ↗

Requirements

  • Only the author’s Skill metadata is included. Read the full instructions to check dependencies, licensing and conditions of use.

A small first task

先阅读原作者说明,再决定是否在自己的 AI 工具中使用。

Instructions for your AI tool

Author documentation is not live verification by this site. Without evidence, we do not mark a resource as compatible, safe or free.

Sources and evidence

This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.

Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T15:01:42.663Z.

Categories are inferred from names, descriptions and paths. They do not establish compatibility.

License and attribution

MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.

Original license ↗

Synchronization, copying and user-reported success do not mark a resource as runtime-tested.

Limitations and notes

  • Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.