Peter Steinberger

Swiftui View Refactor

开发编程v1.0.0
作者:Peter Steinberger更新于 2026-05-11swiftui-view-refactor@clawhub_steipete/swiftui-view-refactor
代码生成代码审查

据 AlphaMoat 统计,截至 2026-09,Swiftui View Refactor 累计下载 3,953 次、获 6 个星标,在全部 5,078 个 AI 技能中排名第 2,610 位,在「开发编程」分类(577 个)中排名第 340 位。

累计安装
129
累计下载
3,953
星标
6
全站排名
#2,610
全站前 52% · 「开发编程」前 59%
平台累计安装累计下载星标评论
ClawHub ↗1283,9286-
SkillHub ↗1293,9536—

各平台的计数口径不同,统计时间不一致。

这个技能是做什么的

该指南整理视图属性与方法顺序,偏好轻量视图和 MV 组织方式,并关注依赖注入与 Observation 状态传递。其架构建议具有明确倾向,未必适合每个代码库;应用前应对照项目现有约定,逐项审阅差异,确保布局、行为和业务逻辑没有被机械式重构改变。

资料发布于 2026-10-29

可以用来做什么

  • 统一 SwiftUI 视图文件的组织结构
  • 检查依赖注入和状态传递方式
  • 在符合项目约定时整理视图代码

什么时候用

  • 用户要求审查或整理 SwiftUI 视图时
  • 目标项目允许采用其偏好的 MV 结构时
  • 能够检查代码差异并运行项目现有验证时

示例提示词

以下是根据技能文档生成的示例,不代表实际运行结果。

请按本项目现有约定审查这个 SwiftUI 视图,先给出拟调整的结构和差异;不要改变布局、行为或业务逻辑,等待我确认后再修改。

月度数据趋势

累计下载3,953
2,6063,3304,0532026-03 · 累计下载 2,7062026-05 · 累计下载 3,2362026-06 · 累计下载 3,3932026-07 · 累计下载 3,6582026-08 · 累计下载 3,7952026-09 · 累计下载 3,95326-0326-09
星标6
5672026-03 · 星标 62026-05 · 星标 62026-06 · 星标 62026-07 · 星标 62026-08 · 星标 62026-09 · 星标 626-0326-09

2026-09 起统计方法调整:同名技能按作者分别统计,并覆盖全部技能。图中虚线为调整前的月份,与之后的数据不直接比较,也不计算跨越调整的增长。

月份累计下载环比星标
2026-093,953—6
2026-083,795+3.7%6
2026-073,658+7.8%6
2026-063,393+4.9%6
2026-053,236—6
2026-032,706—6

SKILL.md 原文

以下是作者发布的原始文件,按纯文本显示,本站未做修改或审核;其中的链接不可点击,命令请核实后再运行。抓取于 2026-10-23。

4.7 KB · ClawHub v1.0.0
---
name: swiftui-view-refactor
description: Refactor and review SwiftUI view files for consistent structure, dependency injection, and Observation usage. Use when asked to clean up a SwiftUI view’s layout/ordering, handle view models safely (non-optional when possible), or standardize how dependencies and @Observable state are initialized and passed.
---

# SwiftUI View Refactor

_Attribution: copied from @Dimillian’s `Dimillian/Skills` (2025-12-31)._

## Overview
展开全部(共 135 行)
Apply a consistent structure and dependency pattern to SwiftUI views, with a focus on ordering, Model-View (MV) patterns, careful view model handling, and correct Observation usage.

## Core Guidelines

### 1) View ordering (top → bottom)
- Environment
- `private`/`public` `let`
- `@State` / other stored properties
- computed `var` (non-view)
- `init`
- `body`
- computed view builders / other view helpers
- helper / async functions

### 2) Prefer MV (Model-View) patterns
- Default to MV: Views are lightweight state expressions; models/services own business logic.
- Favor `@State`, `@Environment`, `@Query`, and `task`/`onChange` for orchestration.
- Inject services and shared models via `@Environment`; keep views small and composable.
- Split large views into subviews rather than introducing a view model.

### 3) Split large bodies and view properties
- If `body` grows beyond a screen or has multiple logical sections, split it into smaller subviews.
- Extract large computed view properties (`var header: some View { ... }`) into dedicated `View` types when they carry state or complex branching.
- It's fine to keep related subviews as computed view properties in the same file; extract to a standalone `View` struct only when it structurally makes sense or when reuse is intended.
- Prefer passing small inputs (data, bindings, callbacks) over reusing the entire parent view state.

Example (extracting a section):

```swift
var body: some View {
    VStack(alignment: .leading, spacing: 16) {
        HeaderSection(title: title, isPinned: isPinned)
        DetailsSection(details: details)
        ActionsSection(onSave: onSave, onCancel: onCancel)
    }
}
```

Example (long body → shorter body + computed views in the same file):

```swift
var body: some View {
    List {
        header
        filters
        results
        footer
    }
}

private var header: some View {
    VStack(alignment: .leading, spacing: 6) {
        Text(title).font(.title2)
        Text(subtitle).font(.subheadline)
    }
}

private var filters: some View {
    ScrollView(.horizontal, showsIndicators: false) {
        HStack {
            ForEach(filterOptions, id: \.self) { option in
                FilterChip(option: option, isSelected: option == selectedFilter)
                    .onTapGesture { selectedFilter = option }
            }
        }
    }
}
```

Example (extracting a complex computed view):

```swift
private var header: some View {
    HeaderSection(title: title, subtitle: subtitle, status: status)
}

private struct HeaderSection: View {
    let title: String
    let subtitle: String?
    let status: Status

    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            Text(title).font(.headline)
            if let subtitle { Text(subtitle).font(.subheadline) }
            StatusBadge(status: status)
        }
    }
}
```

### 4) View model handling (only if already present)
- Do not introduce a view model unless the request or existing code clearly calls for one.
- If a view model exists, make it non-optional when possible.
- Pass dependencies to the view via `init`, then pass them into the view model in the view's `init`.
- Avoid `bootstrapIfNeeded` patterns.

Example (Observation-based):

```swift
@State private var viewModel: SomeViewModel

init(dependency: Dependency) {
    _viewModel = State(initialValue: SomeViewModel(dependency: dependency))
}
```

### 5) Observation usage
- For `@Observable` reference types, store them as `@State` in the root view.
- Pass observables down explicitly as needed; avoid optional state unless required.

## Workflow

1) Reorder the view to match the ordering rules.
2) Favor MV: move lightweight orchestration into the view using `@State`, `@Environment`, `@Query`, `task`, and `onChange`.
3) If a view model exists, replace optional view models with a non-optional `@State` view model initialized in `init` by passing dependencies from the view.
4) Confirm Observation usage: `@State` for root `@Observable` view models, no redundant wrappers.
5) Keep behavior intact: do not change layout or business logic unless requested.

## Notes

- Prefer small, explicit helpers over large conditional blocks.
- Keep computed view builders below `body` and non-view computed vars above `init`.
- For MV-first guidance and rationale, see `references/mv-patterns.md`.

「开发编程」分类下的热门技能

Peter Steinberger 的其他技能

数据月份:2026-09 · 下载/星标/安装数聚合自公开技能注册库(ClawHub、SkillHub),按月更新。口径说明见数据方法。