Swift Architecture

作者 dpearson26998d90fd121a26無授權條款1.1K 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫2 個月前更新

Selects, reviews, and migrates Apple-platform app architectures across MV with Observation, MVVM, MVI, TCA, Clean Architecture, Coordinator, and legacy VIPER. Use when choosing module and dependency boundaries, escalating a feature beyond simple SwiftUI MV, planning incremental architecture migration, or auditing state ownership and test seams.

AI 產生的概覽

指導 Apple 平台應用程式架構的選擇、審查與漸進式移轉,涵蓋 MV、MVVM、MVI、TCA、Clean Architecture、Coordinator 與 VIPER。

功能
此技能提供一套決策流程,用來挑選最小的架構,讓 Swift 與 SwiftUI 應用程式中的狀態歸屬、相依性、副作用與測試接縫變得明確。內容涵蓋模式選擇準則、升級訊號、移轉步驟、常見錯誤與審查清單,並指向一份包含詳細模式結構的參考檔案。它產出的是架構指引與移轉方案,而非程式碼成品。
適用情境
適用於為 Apple 平台功能選擇模組與相依性邊界、判斷是否需要從單純的 SwiftUI MV 升級、規劃漸進式架構移轉,或稽核狀態歸屬與測試接縫時。
執行需求
不需要指令碼或特殊工具,僅為說明性技能。它引用隨附的 Markdown 參考檔案,並連結到 Apple 與 TCA 的外部文件,因此存取這些連結可能需要網路。

Swift Architecture

Choose the smallest architecture that makes state ownership, dependencies, side effects, and tests explicit. Default new SwiftUI features to MV; escalate only for observed complexity.

Contents

Scope Boundary

This skill owns pattern selection, module boundaries, dependency direction, migration strategy, and architecture-level test seams. Route SwiftUI property-wrapper wiring and view composition to swiftui-patterns, navigation APIs and route models to swiftui-navigation, isolation diagnostics to swift-concurrency, and test syntax/fixtures to swift-testing.

Decision Workflow

  1. Record the feature's state owner, inputs, outputs, dependencies, side effects, navigation handoffs, and current tests.
  2. Identify the concrete pressure: complex state machine, shared derived state, dependency control, feature composition, team ownership, or UIKit navigation.
  3. Select the smallest pattern that addresses that pressure; write down what it adds and what remains unchanged.
  4. Implement one vertical slice with injected dependencies and observable state transitions.
  5. Run existing behavior tests plus state-transition and dependency-failure tests. If behavior changes, restore the fixture, fix the smallest boundary, and rerun before migrating another slice.

Pattern Selection

PatternChoose whenMain cost
MVSwiftUI feature has straightforward state and orchestrationLogic can drift into large views without decomposition
MVVMPresentation logic needs an independently testable adapterExtra layer can become a forwarding shell
MVIA feature is best modeled as explicit state + intents + reducer/effectsBoilerplate and centralized transition design
TCAMany composable features need deterministic effects, dependencies, and testingFramework learning and architectural commitment
Clean ArchitectureLarge product needs strict dependency direction across domain/data/UIProtocol and mapping overhead
CoordinatorUIKit or hybrid navigation needs a separate flow ownerAnother lifecycle and routing owner
VIPERMaintaining an existing UIKit module with established VIPER boundariesVery high ceremony; poor default for new SwiftUI work

Use Coordinator alongside another state pattern when navigation complexity is the pressure; it is not a replacement for domain/state architecture.

MV Default

Keep views as state expressions and put business operations in observable models and injected services:

swift
@MainActor@Observablefinal class TripStore {    private let client: TripClient    var trips: [Trip] = []    var error: Error?
    init(client: TripClient) { self.client = client }
    func load() async {        do { trips = try await client.fetchTrips() }        catch { self.error = error }    }}
struct TripList: View {    @State private var store: TripStore
    init(client: TripClient) {        _store = State(initialValue: TripStore(client: client))    }
    var body: some View {        List(store.trips) { Text($0.name) }            .task { await store.load() }    }}

Load Architecture Pattern Recipes [blocked] for MVVM, MVI, TCA, Clean Architecture, Coordinator, and VIPER structure.

Escalation Signals

  • Choose MVVM when substantial presentation transformation must be tested without rendering and the adapter has real behavior.
  • Choose MVI when transitions, invalid states, and effects need one auditable reducer-like path.
  • Choose TCA when feature composition, dependency overrides, cancellation, and deterministic effect tests recur across modules.
  • Choose Clean Architecture when independent domain rules and dependency direction matter across multiple delivery/data layers.
  • Add Coordinator for UIKit/hybrid route ownership, deep flow composition, or conditional navigation outside view controllers.
  • Keep VIPER for compatible legacy modules or deliberate migrations; do not start a new SwiftUI feature with it by habit.

Do not escalate merely because a view is long. First extract subviews, services, and focused observable models.

Migration

Migrate one feature boundary at a time:

  1. Freeze behavior with tests and a dependency/state inventory.
  2. Introduce the target boundary around existing operations.
  3. Move one state transition or dependency at a time without rewriting UI and persistence simultaneously.
  4. Compare behavior, navigation, cancellation, error, and persistence results after each slice.
  5. Remove the old path only after no callers or tests depend on it.

For ObservableObject to Observation, preserve the same owner and mutation isolation before replacing wrappers. For MVVM to MV, delete forwarding view-model members only after views bind to the same model/service behavior. For TCA adoption, wrap one feature's state/actions/effects and migrate dependencies incrementally.

Common Mistakes

MistakeFix
Pattern chosen by popularityTie it to an observed feature pressure.
View model only forwards propertiesRemove it and use MV.
One object owns navigation, networking, formatting, persistence, and UI stateSplit by responsibility and dependency direction.
TCA or Clean Architecture applied to trivial screensStart with MV and preserve an escalation seam.
Coordinator used as a state architectureKeep it focused on route/lifecycle ownership.
Multiple patterns mixed inside one featureDefine one local state/effect model and migrate at feature boundaries.
Big-bang migrationMove one tested vertical slice and rerun the same proof matrix.

Review Checklist

  • Choice is justified by concrete feature/team pressures
  • State owner, mutation path, dependencies, effects, and navigation owner are explicit
  • Dependencies are injected and replaceable in tests
  • Pattern cost is proportional to feature complexity
  • UI mechanics, navigation APIs, isolation, and test syntax route to sibling skills
  • Migration preserves behavior one vertical slice at a time
  • Failure, cancellation, navigation, and persistence behavior are verified after each slice
  • No forwarding-only layers or god objects remain

References

來源與署名

來源:dpearson2699/swift-ios-skills位於skills/swift-architecture提交8d90fd1

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架

更多來自 dpearson2699/swift-ios-skills 的技能

Widgetkit

dpearson2699

指導實作、審查與改進 iOS、iPadOS、watchOS 與 CarPlay 上的 WidgetKit 小工具與控制項。

Software Development1.1K2 個月前更新

Weatherkit

dpearson2699

指導 iOS 開發者使用 WeatherService 取得 WeatherKit 預報、警報與署名資訊。

Software Development1.1K2 個月前更新

Vision Framework

dpearson2699

Implement computer vision features including text recognition (OCR), face detection, barcode scanning, image segmentation, object tracking, and document scanning in iOS apps. Covers both the modern Swift-native Vision API (iOS 18+) and legacy VNRequest patterns, VisionKit DataScannerViewController for live camera scanning, and CoreMLRequest/VNCoreMLRequest for custom model inference. Use when adding OCR, barcode scanning, face detection, or custom Core ML model inference with Vision.

待分類1.1K2 個月前更新

Tipkit

dpearson2699

Implement and review Apple TipKit feature-discovery UI for iOS 17+ apps. Use when adding or auditing in-app tips, contextual help, coach marks, Tip, TipView, popoverTip, rules, events, actions, display frequency, testing overrides, reusable tip identifiers, or iOS 18+ TipGroup and CloudKit tip sync; avoid for generic SwiftUI navigation or layout outside tip presentation.

待分類1.1K2 個月前更新

Tabletopkit

dpearson2699

指導使用 TabletopKit 在 visionOS 上打造多人空間桌遊,涵蓋棋具、座位、動作與 RealityKit 算繪。

Software Development1.1K2 個月前更新

Swiftui Webkit

dpearson2699

指導在 iOS 26 及更新版本的 SwiftUI App 中使用 WebKit for SwiftUI 嵌入與控制網頁內容。

Software Development1.1K2 個月前更新