swiftui-ui-patterns by dimillian/skills
npx skills add https://github.com/dimillian/skills --skill swiftui-ui-patterns根据你的目标选择相应的路径:
rg "TabView\(" 或类似命令查找相近的示例,然后阅读最接近的 SwiftUI 视图。references/components-index.md 中选择相关的组件参考并遵循其指导。references/scroll-reveal.md。references/app-wiring.md 开始,以连接 TabView + NavigationStack + sheets。AppTab 和 RouterPath。广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
@State、@Binding、@Observable、@Environment),避免不必要的视图模型。ObservableObject:根所有权用 @StateObject,注入观察用 @ObservedObject,仅对真正共享的应用级状态使用 @EnvironmentObject。.task 以及显式的加载/错误状态。关于重启、取消和防抖的指导,请阅读 references/async-state.md。@Environment 中,但对于功能局部的依赖项和模型,更倾向于显式的初始化器注入。关于根连接模式,请阅读 references/app-wiring.md。.sheet(item:) 而非 .sheet(isPresented:)。避免在 sheet 主体内部使用 if let。Sheets 应拥有其操作,并在内部调用 dismiss(),而不是转发 onCancel/onConfirm 闭包。使用与所有权模型匹配的最窄范围状态工具:
| 场景 | 首选模式 |
|---|---|
| 由一个视图拥有的本地 UI 状态 | @State |
| 子视图修改父视图拥有的值状态 | @Binding |
| iOS 17+ 上根视图拥有的引用模型 | 使用 @Observable 类型的 @State |
iOS 17+ 上子视图读取或修改注入的 @Observable 模型 | 将其作为存储属性显式传递 |
| 共享的应用服务或配置 | @Environment(Type.self) |
| iOS 16 及更早版本的遗留引用模型 | 根视图使用 @StateObject,注入时使用 @ObservedObject |
首先选择所有权位置,然后选择包装器。当普通值状态足够时,不要引入引用模型。
references/navigationstack.md:导航所有权、每个标签页的历史记录和枚举路由。references/sheets.md:集中式模态呈现和枚举驱动的 sheets。references/deeplinks.md:URL 处理以及将外部链接路由到应用目的地。references/app-wiring.md:根依赖图、环境使用和应用外壳连接。references/async-state.md:.task、.task(id:)、取消、防抖和异步 UI 状态。references/previews.md:#Preview、夹具、模拟环境和隔离的预览设置。references/performance.md:稳定的标识、观察范围、惰性容器和渲染成本护栏。body 驱动的代码路径中进行实时服务调用,而不是使用视图生命周期钩子或注入的模型/服务。AnyView,而这些问题本应通过更好的组合来解决。@EnvironmentObject 或全局路由器。@Environment,哪些应作为显式的初始化器输入。references/navigationstack.md、references/sheets.md 或 references/deeplinks.md。在继续之前,构建并确认没有编译器错误。.task 或 .task(id:) 实现异步加载,并在需要时添加显式的加载和错误状态。当工作依赖于变化的输入或取消时,请阅读 references/async-state.md。references/previews.md。references/performance.md。对于常见的 SwiftUI 编译错误——缺少 @State 注解、模糊的 ViewBuilder 闭包或不匹配的泛型类型——在更新调用点之前解决它们。如果构建失败: 仔细阅读错误信息,修复识别出的问题,然后重新构建再继续下一步。如果预览崩溃,请隔离有问题的子视图,确认其状态初始化有效,并在继续之前重新运行预览。使用 references/components-index.md 作为入口点。每个组件参考应包含:
references/<component>.md。references/components-index.md 中更新新条目。每周安装量
1.5K
代码库
GitHub 星标数
2.3K
首次出现
2026 年 1 月 20 日
安全审计
安装于
opencode1.2K
codex1.2K
gemini-cli1.2K
claude-code1.2K
cursor1.0K
github-copilot979
Choose a track based on your goal:
rg "TabView\(" or similar, then read the closest SwiftUI view.references/components-index.md and follow its guidance.references/scroll-reveal.md before implementing gestures manually.references/app-wiring.md to wire TabView + NavigationStack + sheets.AppTab and RouterPath based on the provided skeletons.@State, @Binding, @Observable, @Environment) and avoid unnecessary view models.ObservableObject with @StateObject for root ownership, @ObservedObject for injected observation, and @EnvironmentObject only for truly shared app-level state..task and explicit loading/error states. For restart, cancellation, and debouncing guidance, read references/async-state.md.Use the narrowest state tool that matches the ownership model:
| Scenario | Preferred pattern |
|---|---|
| Local UI state owned by one view | @State |
| Child mutates parent-owned value state | @Binding |
| Root-owned reference model on iOS 17+ | @State with an @Observable type |
Child reads or mutates an injected @Observable model on iOS 17+ | Pass it explicitly as a stored property |
| Shared app service or configuration | @Environment(Type.self) |
| Legacy reference model on iOS 16 and earlier |
Choose the ownership location first, then pick the wrapper. Do not introduce a reference model when plain value state is enough.
references/navigationstack.md: navigation ownership, per-tab history, and enum routing.references/sheets.md: centralized modal presentation and enum-driven sheets.references/deeplinks.md: URL handling and routing external links into app destinations.references/app-wiring.md: root dependency graph, environment usage, and app shell wiring.references/async-state.md: .task, .task(id:), cancellation, debouncing, and async UI state.references/previews.md: #Preview, fixtures, mock environments, and isolated preview setup.body-driven code paths instead of view lifecycle hooks or injected models/services.AnyView to work around type mismatches that should be solved with better composition.@EnvironmentObject or a global router without a clear ownership reason.@Environment and which should stay as explicit initializer inputs.references/navigationstack.md, references/sheets.md, or references/deeplinks.md. Build and verify no compiler errors before proceeding..task or .task(id:), plus explicit loading and error states when needed. Read references/async-state.md when the work depends on changing inputs or cancellation.references/previews.md when the view needs fixtures or injected mock dependencies.Use references/components-index.md as the entry point. Each component reference should include:
references/<component>.md.references/components-index.md with the new entry.Weekly Installs
1.5K
Repository
GitHub Stars
2.3K
First Seen
Jan 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode1.2K
codex1.2K
gemini-cli1.2K
claude-code1.2K
cursor1.0K
github-copilot979
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
102,200 周安装
@Environment, but prefer explicit initializer injection for feature-local dependencies and models. For root wiring patterns, read references/app-wiring.md..sheet(item:) over .sheet(isPresented:) when state represents a selected model. Avoid if let inside a sheet body. Sheets should own their actions and call dismiss() internally instead of forwarding onCancel/onConfirm closures.@StateObject at the root, @ObservedObject when injected |
references/performance.md: stable identity, observation scope, lazy containers, and render-cost guardrails.references/performance.md if the screen is large, scroll-heavy, or frequently updated. For common SwiftUI compilation errors — missing @State annotations, ambiguous ViewBuilder closures, or mismatched generic types — resolve them before updating callsites. If the build fails: read the error message carefully, fix the identified issue, then rebuild before proceeding to the next step. If a preview crashes, isolate the offending subview, confirm its state initialisation is valid, and re-run the preview before continuing.